poj_2253Frogger &&poj_1797Heavy Transportation
Frogger
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20181 | Accepted: 6542 |
Description
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her
by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's
stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line
after each test case, even after the last one.
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414题目的意思是最大的跳跃尽可能小,DIJKSTRA的变型。
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<cmath> #include<vector> #include<algorithm> #include<set> #include<string> #include<queue> #include <stack> using namespace std; #pragma warning(disable : 4996) const int MAXN = 205; #define INF 999999 typedef struct Point { int x; int y; }Point; int n; double maps[MAXN][MAXN]; bool visited[MAXN]; double dist[MAXN]; double ans; void init() { memset(visited, false, sizeof(visited)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if(i == j) { maps[i][j] = 0; } else { maps[i][j] = INF; } } } } void Dijkstra(int s, int e) //起点,终点 { int i, j; double minValue; int minNode; dist[s] = 0; visited[s] = true; for (i = 1; i <= n; i++) { dist[i] = maps[s][i]; } for (i = 1; i <= n; i++) { minValue = INF; minNode = 0; for (j = 1; j <= n; j++) { if(!visited[j] && minValue > dist[j]) { minNode = j; minValue = dist[j]; } } if(minNode == 0) { break; } if(minValue > ans) { ans = minValue; } if(minNode == e) { break; } visited[minNode] = true; for (j = 1; j <= n; j++) { if(!visited[j] && maps[minNode][j] != INF) { if(maps[minNode][j] < dist[j]) { dist[j] = maps[minNode][j]; } } } } } int main() { freopen("in.txt", "r", stdin); double dis; Point num[MAXN]; int count = 1; while(scanf("%d", &n) != EOF) { if(n == 0) { break; } init(); for(int i = 1; i <= n; i++) { scanf("%d %d", &num[i].x, &num[i].y); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dis = sqrt((num[i].x - num[j].x) * (num[i].x - num[j].x) + (num[i].y - num[j].y) * (num[i].y - num[j].y)); if(maps[i][j] > dis) { maps[i][j] = dis; maps[j][i] = dis; } } } ans = 0; Dijkstra(1, 2); printf("Scenario #%d\nFrog Distance = %.3f\n\n", count++, ans); } return 0; }
Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 17241 | Accepted: 4529 |
Description
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing
of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for
the scenario with a blank line.
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<cmath> #include<vector> #include<algorithm> #include<set> #include<string> #include<queue> #include <stack> using namespace std; #pragma warning(disable : 4996) const int MAXN = 1005; const int INF = 999999; int n; int maps[MAXN][MAXN]; bool visited[MAXN]; int dist[MAXN]; int ans; void init() { memset(visited, false, sizeof(visited)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if(i == j) { maps[i][j] = 0; } else { maps[i][j] = -INF; } } } } void Dijkstra(int s, int e) //起点,终点 { int i, j; int minValue, minNode; dist[s] = 0; visited[s] = true; for (i = 1; i <= n; i++) { dist[i] = maps[s][i]; } for (i = 1; i <= n; i++) { minValue = -INF; minNode = 0; for (j = 1; j <= n; j++) { if(!visited[j] && minValue < dist[j]) { minNode = j; minValue = dist[j]; } } if(minNode == 0) { break; } if(minValue < ans) { ans = minValue; } if(minNode == e) { return; } visited[minNode] = true; for (j = 1; j <= n; j++) { if(!visited[j] && maps[minNode][j] != -INF) { if(maps[minNode][j] > dist[j]) { dist[j] = maps[minNode][j]; } } } } } int main() { freopen("in.txt", "r", stdin); int t, dis, m, x, y; int count = 1; scanf("%d", &t); while(t--) { scanf("%d %d", &n, &m); init(); while (m--) { scanf("%d %d %d", &x, &y, &dis); if(maps[x][y] < dis) { maps[x][y] = dis; maps[y][x] = dis; } } ans = INF; Dijkstra(1, n); printf("Scenario #%d:\n%d\n\n", count++, ans); } return 0; }
Keep it simple!