Silver Cow Party--poj3268(SPFA)

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15533   Accepted: 7045

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; 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: NM, and X

Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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.

题意大概是:有1-N头牛住在不同的地方,现在所有的牛要去参加银牛party,地点在X

有M条路(单向的)所有的牛都很懒,都要走最短的路去,最短的路回,求走的路最长的那头牛走的路程!

这个题挺好的!

要反向建图求去时的路程,正向建图求回来的路程!

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define MAXN 1000+10
#define MAXM 200000+10
using namespace std;

int head[MAXN],vis[MAXN],dis[MAXN];
int n,m,x;
struct node
{
	int from,to,weight,next;
 } edge[MAXM];
 void input()
 {
 	int i,a,c,b;
 	memset(head,-1,sizeof(head));
 	for(i=0;i<m;i++)
 	{
 		scanf("%d%d%d",&a,&b,&c);//反向建图
 		//edge[i]={b,a,c,head[b]};//这种写法在poj过不了!害我ce两次
 		edge[i].from=b;
 		edge[i].to=a;
 		edge[i].weight=c;
 		edge[i].next=head[b];
 		head[b]=i;
	 }
 }
 void spfa()
 {
 	memset(dis,INF,sizeof(dis));
 	memset(vis,0,sizeof(vis));
 	dis[x]=0;
 	vis[x]=1;
 	queue<int> q;
	q.push(x);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int k=head[u];k!=-1;k=edge[k].next)
		{
			int v=edge[k].to;
			if(dis[v]>dis[u]+edge[k].weight)
			{
				dis[v]=dis[u]+edge[k].weight;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	 }
 }
 int main()
 {
 	int a[MAXN],b[MAXN];
 	while(scanf("%d%d%d",&n,&m,&x)!=EOF)
 	{
 		int i;
 		input();
 		spfa();
 		for( i=1;i<=n;i++)
 		//printf("++%d ",dis[i]);
 		a[i]=dis[i];
 		int t;
		memset(head,-1,sizeof(head));
 		for(i=0;i<m;i++)//正向建图
 		{
 			t=edge[i].from;
 			edge[i].from=edge[i].to;
 			edge[i].to=t;
 			edge[i].next=head[edge[i].from];
 			head[edge[i].from]=i;
		 }
		 spfa();
		for( i=1;i<=n;i++)
		a[i]+=dis[i];//往返路程相加

		int m=-1;
 		for(i=1;i<=n;i++)
 		m=m < a[i] ? a[i]:m;//求出最大的路程
 		printf("%d\n",m);
	 }
	 return 0;
 }
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 20:50:16

Silver Cow Party--poj3268(SPFA)的相关文章

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 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 

Silver Cow Party.(POJ-3268)

本来想用Floyd算法,可惜超时,毕竟复杂度太高,而且并没有必要求出任意两点间的最短距离. 求两点间的最短路有两种方法,dijkstra和Bellman ,前者不能有负圈,后者可以有负圈,另外,Floyd也可以求带负圈的最短距离. 我们只需要求出x到其他个点的最短距离和个点到它的最短距离就行了.当然,我所写的还求了很多多余的量,是可以优化的. #include<cstdio> #include<cstring> #include<iostream> #include&l

POJ 3268 Silver Cow Party(SPFA)

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; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 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 

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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 ≤

POJ3268 Silver Cow Party 【Dijkstra】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13183   Accepted: 5932 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 

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 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

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co