Silver Cow Party SPFA+SLF优化 单源起点终点最短路
2017-08-02 14:56
477 查看
Silver Cow Party
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; road i 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
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai,Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题意:cow很懒。。他们要参加一个party,想知道从最短路往返走哪头cow用时最多,所有的cow都会卡着点到达并返回。。已知一点,求其他点到这点再返回各点沿最短路走的最大用时。
思路:这次练了练SPFA+SLF(双向队列优化)。之前做的单源最短路是从一个点出发,到各点的最短路,这道题加上了从所有点出发到一个点的最短路,只需将有向图倒着存储,正着找就可以了。
#include<stdio.h> #include<string.h> #include<deque> #include<vector> #define MAX 1005 #define INF 0x3f3f3f3f using namespace std; struct Node{ int v,w; }node; vector<Node> edge[MAX],redge[MAX]; int dis[MAX],diss[MAX],b[MAX]; int n; void spfa(int k) { int i; deque<int> q; //双向队列 for(i=1;i<=n;i++){ dis[i]=INF; diss[i]=INF; } memset(b,0,sizeof(b)); b[k]=1; dis[k]=0; q.push_back(k); while(q.size()){ int u=q.front(); for(i=0;i<edge[u].size();i++){ int v=edge[u][i].v; int w=edge[u][i].w; if(dis[v]>dis[u]+w){ dis[v]=dis[u]+w; if(b[v]==0){ b[v]=1; if(dis[v]>dis[u]) q.push_back(v); //SLF else q.push_front(v); } } } b[u]=0; q.pop_front(); } b[k]=1; diss[k]=0; q.push_back(k); while(q.size()){ int u=q.front(); for(i=0;i<redge[u].size();i++){ int v=redge[u][i].v; int w=redge[u][i].w; if(diss[v]>diss[u]+w){ diss[v]=diss[u]+w; if(b[v]==0){ b[v]=1; if(diss[v]>diss[u]) q.push_back(v); //SLF else q.push_front(v); } } } b[u]=0; q.pop_front(); } } int main() { int m,x,u,v,w,i; scanf("%d%d%d",&n,&m,&x); for(i=1;i<=n;i++){ edge[i].clear(); redge[i].clear(); } for(i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); node.v=v; node.w=w; edge[u].push_back(node); node.v=u; redge[v].push_back(node); } spfa(x); int max=0; for(i=1;i<=n;i++){ if(dis[i]+diss[i]>max&&dis[i]!=INF&&diss[i]!=INF) max=dis[i]+diss[i]; } printf("%d\n",max); return 0; }
相关文章推荐
- poj-3268 Silver Cow Party (最短路-spfa-求到同一起点,同一终点的最短路)
- POJ - 3268 Silver Cow Party解题报告(dijkstra分别求单源起点和单源终点的最短路)
- POJ 3268 Silver Cow Party 正反单源最短路
- 暑假-最短路(Bellman-ford、spfa)-E - Silver Cow Party
- 文章标题 POJ 3268 : Silver Cow Party (最短路--spfa)
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
- POJ3268 Silver Cow Party spfa求解 最短路
- poj 3268 Silver Cow Party(最短路+SPFA)
- D - Silver Cow Party——最短路_spfa()算法
- poj 3268 Silver Cow Party(最短路+SPFA)
- POJ - 3268 Silver Cow Party (往返最短路,Floyd,Dijkstra 2次优化)
- 最短路 poj 3268 Silver Cow Party
- HDU 1535 Invitation Cards (最短路,附SLF优化SPFA)
- POJ3268 Silver Cow Party【最短路】
- POJ--3268--Silver Cow Party【SPFA+邻接表】
- POJ - 3268 Silver Cow Party (俩遍最短路)
- poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
- POJ 3268 Silver Cow Party(最短路dijkstra)
- POJ 3268 Silver Cow Party 最短路
- kuangbin专题最短路 D - Silver Cow Party