Dijkstra求解最短路
来源:互联网 发布:2016网络播放器软件 编辑:程序博客网 时间:2024/06/06 02:25
poj2394:http://poj.org/problem?id=2394
Checking an Alibi
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6686 Accepted: 2432
Description
A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1 <= C <= 100) cows is the culprit. Fortunately, a passing satellite took an image of his farm M (1 <= M <= 70000) seconds before the crime took place, giving the location of all of the cows. He wants to know which cows had time to get to the barn to steal the grain.
Farmer John's farm comprises F (1 <= F <= 500) fields numbered 1..F and connected by P (1 <= P <= 1,000) bidirectional paths whose traversal time is in the range 1..70000 seconds (cows walk very slowly). Field 1 contains the barn. It takes no time to travel within a field (switch paths).
Given the layout of Farmer John's farm and the location of each cow when the satellite flew over, determine set of cows who could be guilty.
NOTE: Do not declare a variable named exactly 'time'. This will reference the system call and never give you the results you really want.
Farmer John's farm comprises F (1 <= F <= 500) fields numbered 1..F and connected by P (1 <= P <= 1,000) bidirectional paths whose traversal time is in the range 1..70000 seconds (cows walk very slowly). Field 1 contains the barn. It takes no time to travel within a field (switch paths).
Given the layout of Farmer John's farm and the location of each cow when the satellite flew over, determine set of cows who could be guilty.
NOTE: Do not declare a variable named exactly 'time'. This will reference the system call and never give you the results you really want.
Input
* Line 1: Four space-separated integers: F, P, C, and M
* Lines 2..P+1: Three space-separated integers describing a path: F1,F2, and T. The path connects F1 and F2 and requires T seconds to traverse.
* Lines P+2..P+C+1: One integer per line, the location of a cow. The first line gives the field number of cow 1, the second of cow 2, etc.
* Lines 2..P+1: Three space-separated integers describing a path: F1,F2, and T. The path connects F1 and F2 and requires T seconds to traverse.
* Lines P+2..P+C+1: One integer per line, the location of a cow. The first line gives the field number of cow 1, the second of cow 2, etc.
Output
* Line 1: A single integer N, the number of cows that could be guilty of the crime.
* Lines 2..N+1: A single cow number on each line that is one of the cows that could be guilty of the crime. The list must be in ascending order.
* Lines 2..N+1: A single cow number on each line that is one of the cows that could be guilty of the crime. The list must be in ascending order.
Sample Input
7 6 5 81 4 21 2 12 3 63 5 55 4 61 7 914537
Sample Output
41234
Hint
INPUT DETAILS:
Fields/distances like this:
OUTPUT DETAILS:
Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.
Fields/distances like this:
6 4------5 | | 2| | | |7-----1 |5 9 | | 1| | | | 2------3
OUTPUT DETAILS:
Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.
题意理解很费劲啊。。。。。大概就是说有F个农场C头牛,P条路,给出前M秒牛的位置,问有哪些牛能到达农场1偷吃粮食。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <map>#include <cmath>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const int MAXN = 0x3f3f3f3f;int f,p,c,m;int g[550][550];int dis[1100],vis[1100],V[150];void Dij(int x){ for(int i = 1; i <= f; i++) { dis[i] = MAXN; vis[i] = 0; } dis[x] = 0; for(int i = 1; i <= f; i++) { int MIN = MAXN; int u = x; for(int j = 1; j <= f; j++) { if(!vis[j] && dis[j] < MIN) { u = j; MIN = dis[j]; } } vis[u] = 1; for(int j = 1; j <= f; j++) { if(!vis[j] && (MIN + g[u][j]) < dis[j]) { dis[j] = MIN + g[u][j]; } } }}int main(){// freopen("in.txt","r",stdin); while(~scanf("%d%d%d%d",&f,&p,&c,&m)) { for(int i = 1; i <= f; i++) { for(int j = 1; j <= f; j++) g[i][j] = (i == j?0:MAXN); } for(int i = 0; i < p; i++) { int a,b,w; scanf("%d%d%d",&a,&b,&w); g[a][b] = g[b][a] = min(g[a][b],w); // 这里很坑啊,,,涨姿势了 } int k = 0; for(int i = 1; i <= c; i++) { int x; scanf("%d",&x); Dij(x); if(dis[1] <= m)V[k++] = i; } printf("%d\n",k); sort(V,V+k); for(int i = 0; i < k; i++) printf("%d\n",V[i]); } return 0;}
0 0
- Dijkstra求解最短路
- Dijkstra方法求解网络最短路
- 最短路 Dijkstra算法
- 最短路之dijkstra
- hdu1874 Dijkstra 最短路
- HUD最短路 (Dijkstra)
- hdu2544 Dijkstra最短路
- 最短路之Dijkstra
- 图论 最短路 Dijkstra
- Dijkstra 求最短路
- dijkstra最短路 hdu2066
- Dijkstra 求最短路
- 最短路dijkstra--poj2502
- HDU2544:最短路(Dijkstra)
- 最短路 dijkstra模板
- 最短路(dijkstra)
- 最短路算法Dijkstra
- Dijkstra最短路算法
- 推荐算法和机器学习入门
- Debian 8 Tab命令无法自动补全
- Android bluetooth介绍(四): a2dp connect流程分析
- oracle 事务处理详解
- 稳定流畅的APP编写时候的注意事项
- Dijkstra求解最短路
- 基于Struts2文件上传
- tomcat启动时报错:匹配不到默认的(default)servlet
- C#创建服务及使用程序自动安装服务,.NET创建一个即是可执行程序又是Windows服务的exe
- 蓝桥杯 算法提高 快乐司机
- 重载(overloading)和重写(overriding)
- 游戏行业专业术语
- UEditor设置上传图片值允许网络图片和本地图片的方式
- Android—ListView Item 展开动画效果