Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20181 | Accepted: 6542 |
Description
Input
Output
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; }
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 17241 | Accepted: 4529 |
Description
Input
Output
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; }