poj3268Silver Cow Party(单源最短路径)
来源:互联网 发布:u盘数据丢失 编辑:程序博客网 时间:2024/06/05 19:16
题意:
n个牛要到牛x处聚会,聚会完后回到自己的地方,每个牛走的都是最短路,问哪一个牛走的路用的时间最长。方法:以牛x为起点,通过dijiktra算法就可以求出每个牛的返程路径,但每个牛去牛x的路径不可能用floyd算法,所有我们把所有路径返过来,再以牛x为起点,求一次最短路径,就可以知道每个牛的去程时间了。
Silver Cow PartyTime Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers:Ai, Bi, and Ti. The described road runs from farmAi to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#define INF 999999999#define LL long long#define mod 1000003using namespace std;struct Edge{int to, dist;Edge(){}Edge(int v, int d) :to(v), dist(d) {}};struct node{ int from,to,dist;};node road[100005];//记录路径,之后反过来int n, m;vector<Edge> edges[1005];bool done[1005];int d[1005];void init(){for (int i = 0; i <= n; i++)edges[i].clear();}struct HeapNode{int d,u;HeapNode(int dd,int uu){ d=dd; u=uu;}bool operator < (const HeapNode& rhs) const {return d>rhs.d;}};void dijk(int s){ for(int i=1;i<=n;i++) d[i]=INF; d[s]=0;priority_queue<HeapNode>q; memset(done, 0, sizeof(done));q.push(HeapNode( 0,s ));while (!q.empty()){HeapNode x = q.top();q.pop();int u = x.u;if (done[u]) continue;done[u] = true;for (int i = 0; i<edges[u].size(); i++){Edge& e = edges[u][i];if(d[e.to]>d[u]+e.dist) { d[e.to]=d[u]+e.dist; q.push(HeapNode(d[e.to],e.to)); }}}}int main(){ int mm; int x; while(scanf("%d%d%d",&n,&mm,&x)!=EOF){ int a, b, c; init(); int dd[1005]; int sum=-1; for (int i = 0; i<mm; i++) { scanf("%d %d %d", &a, &b, &c); road[i].from=a,road[i].to=b,road[i].dist=c; edges[a].push_back(Edge(b,c)); } dijk(x); for(int i=1;i<=n;i++) dd[i]=d[i]; init();//记得再次初始化 for(int i=0;i<mm;i++) { a=road[i].from,b=road[i].to,c=road[i].dist; edges[b].push_back(Edge(a,c)); } dijk(x); for(int i=1;i<=n;i++) { dd[i]+=d[i]; if(dd[i]>sum) sum=dd[i]; } printf("%d\n",sum);}}
- poj3268Silver Cow Party(单源最短路径)
- POJ3268Silver Cow Party(单源最短路径变形)
- POJ3268Silver Cow Party
- POJ3268Silver Cow Party~Dij算法+矩阵倒置
- poj3268Silver Cow Party之dijkstra解法
- poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)
- poj3268Silver Cow Party——最短路变形
- poj3268 Silver Cow Party 单源最短路径dijkstra算法
- POJ3268 Silver Cow Party(最短路径)
- poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环)
- POJ3268-Silver Cow Party(最短路径)
- 北大ACM3268——Silver Cow Party~~最短路径
- 最短路径__Silver Cow Party ( Poj 3268 )
- poj 3268 Silver Cow Party(最短路径)
- poj3268~Silver Cow Party(dijkstra+路径反转)
- POJ3268---Silver Cow Party (最短路径)
- [POJ](3268)Silver Cow Party ---最短路径(图)
- Silver Cow Party (SPFA+最短路径)
- HTTP协议浅谈
- wordpress 模板页面加载速度慢问题剖析
- 流媒体压力测试rtmp&hls(含推流和拉流)
- 欢迎使用CSDN-markdown编辑器
- 53. Using a Custom Admin URL
- poj3268Silver Cow Party(单源最短路径)
- Message Flood
- Android官网蓝牙样例BluetoothChat(蓝牙聊天室)解析
- bp神经网络 python初探
- 如何远程登录linux服务器
- Unity Mathf 数学运算(C#)
- SQLServer 移除合并文件及文件组
- IM模块--UItextView带有placeholder属性,自动调节高度
- 问题:ajaxSuccess() ajaxComplete() ajaxError()....??? 参数event xhr options的用法