编程练习7(最短路)
来源:互联网 发布:java中常用的api 编辑:程序博客网 时间:2024/04/29 15:50
A:Frogger(POJ2253)
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.
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
我的代码
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;int T,num,n,m;int sal,p;int i,j,k;struct Node{ int x,y;};struct edge{ int u,v; double w;};const double INF = 1e8;Node stone[205];bool visited[205];double d[205];double distance(int a, int b){ return sqrt(double((stone[a].x-stone[b].x)*(stone[a].x-stone[b].x)+(stone[a].y-stone[b].y)*(stone[a].y-stone[b].y)));}void init(){ for (i = 1 ; i <= num; i++) { d[i] = INF; visited[i] = false; } d[1] = 0; visited[1] = true;}void dijstra(){ int v = 1; init(); for (i = 1 ; i <= num; i++) { int min = INF; //找到最小的d for (j = 1; j <= num; j++) { if(!visited[j] && d[j] < min){ min = d[j]; v = j; } } //标记为访问过,更新其他点 visited[v] = true; if (v == 2) { printf("Scenario #%d\n",T); printf("Frog Distance = %.3f\n\n",d[2]); return; } for (j = 1; j <= num ; j++) { if (!visited[j] && d[j] > max(d[v],distance(v,j))) { d[j] = max(d[v],distance(v,j)); } } }}int main() { // freopen("input.txt", "r", stdin); T = 0; while(scanf("%d",&num)!= EOF && num != 0) { T++; for (i = 1; i <= num; i++) { scanf("%d%d",&stone[i].x,&stone[i].y); } dijstra(); } return 0;}
B:Wormholes(POJ3259)
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1.. F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
我的代码
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const int INF = 1000000000;int numf,n,m;int N, M, W;int i,j;struct Edge{ int beg,end; int w;};int d[505];Edge edge[7000];//w表示边数void init(){ for (i = 1; i <=N; i++) { d[i] = INF; } d[1] = 0;}bool bellman(int w){ for (i = 1; i <= N; i++) { for (j = 1; j <= w; j++) { if (d[edge[j].end] > d[edge[j].beg] + edge[j].w ) { d[edge[j].end] = d[edge[j].beg] + edge[j].w; } } } for (j = 1; j <= w; j++) { if (d[edge[j].end] > d[edge[j].beg] + edge[j].w) { return false; } } return true;}int main() { //freopen("input.txt", "r", stdin); scanf("%d",&numf); while(numf--) { init(); scanf("%d%d%d",&N,&M,&W); int k = 1; for (i = 1; i <= M; i++) { scanf("%d%d%d",&edge[k].beg,&edge[k].end,&edge[k].w); k++; edge[k].beg = edge[k-1].end;edge[k].end = edge[k-1].beg;edge[k].w = edge[k-1].w; k++; } for (i = 1; i <= W; i++) { scanf("%d%d%d",&edge[k].beg,&edge[k].end,&edge[k].w); edge[k].w = - edge[k].w; k++; } bool flag = bellman(2*M+W); if (!flag) { printf("YES\n"); }else{ printf("NO\n"); } } return 0;}
- 编程练习7(最短路)
- Tram (最短路练习模板题)
- 蓝桥杯算法练习最短路(spfa)
- 最短路练习
- 编程练习8(多源最短路)
- hdu 1874 最短路练习
- 最短路(最短路)
- String 长春理工(练习赛hdoj diy)最短路
- POJ 1125 Stockbroker Grapevine(最短路练习)
- 最短路 HDU2544(练习模板的好题)
- 蓝桥杯--算法练习:最短路(单源最短路径spfa算法)
- 最短路入门练习 解题报告
- 蓝桥杯练习系统 ALGO-5 最短路
- POJ2502 Subway -DIJKSTRA最短路练习
- POJ2253 Frogger -DIJKSTRA || FLOYD最短路练习
- 2016夏季练习——最短路
- 2016夏季练习——最短路
- 2016夏季练习——最短路
- root用户和sudo使用root权限的区别
- UVa1225 Digit Counting
- Android 自定义控件 详解
- 什么是对象?
- EBS MOAC
- 编程练习7(最短路)
- 编程练习
- Zuul 2 : The Netflix Journey to Asynchronous, Non-Blocking Systems
- css浮动
- 求最大公约数的算法
- acm UVA 227 Puzzle字符串处理
- HTML JavaScript 基础 (下)
- java解析String字符串xml 多级节点NodeList
- CentOS7.2 在GUI下关闭X Server