POJ-3268 Silver Cow Party(最短路,链式前向星存图)

                                         Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29122   Accepted: 13231

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.

Source

USACO 2007 February Silver

题目大意:一共有N个农场,指定一个农场X,其余的农场的牛要来农场X参加Party,每只牛来到农场X以及回去都会选择最短的路线(有向图,来到和:回去的最短路不一定是同一条路径),求这些去参加Party的牛,往返最短路径之和中的最大值。

先来一发TLE(超时版本):

#include<iostream>
#include<algorithm>
#include<climits>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;                                 
const int MAX=1500+10;
const int INF=0x3f3f3f3f;

int Map[MAX][MAX];   //二维数组存图
queue<int> q;
bool vis[MAX];
int dis[MAX];
int SPAF(int from,int to,int n){
	while(!q.empty()) q.pop();
//	int cnt[MAX];

//	memset(cnt,0,sizeof(cnt));
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++) dis[i]=INF;      
	
	dis[from]=0;
	q.push(from);
	vis[from]=true;   //注释掉的cnt,neg是用来判断负环的
//	cnt[from]++;
//	int neg=0;
	
	while(!q.empty()){
		int now=q.front();
		q.pop();
		vis[now]=false;
		for(int i=1;i<=n;i++)
		if(Map[now][i]&&dis[i]>dis[now]+Map[now][i]){
			dis[i]=dis[now]+Map[now][i];
			if(!vis[i]){
				vis[i]=true;
				q.push(i);
//				++cnt[i];
//				if(cnt[i]>n){
//					neg=1;break;
//				} 
			}
		}
//		if(neg) break;
	} 
	//if(neg) return -1;
	return dis[to];
}
int sovle(int x,int n){
	int res=0;
	int num;
	for(int i=1;i<=n;i++){	
			num=SPAF(i,x,n)+SPAF(x,i,n);
			res=max(res,num);
	}
	return res;
}
int main(){
	int n,m,t,x;
	while(scanf("%d%d%d",&n,&m,&x)!=EOF){
		int a,b,c;
		while(m--){
		   scanf("%d%d%d",&a,&b,&c);
		   Map[a][b]=c;
		}
		int ans=sovle(x,n);
		cout<<ans<<endl;
	}
	return 0;
}

哎,看了看大神的存图方式,改成了以下这样:

AC代码:

#include<iostream>
#include<algorithm>
#include<climits>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;                                 
const int MAX_N=10500+10;
const int INF=0x3f3f3f3f;
struct EDGE{
	int next;
	int to;
	int w;
}edge[MAX_N];

int head[MAX_N];
int cnt,n,m;
int dis[MAX_N];
bool vis[MAX_N];
queue<int> q;

void Add(int u,int v,int w){	
	edge[cnt].to=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}

int SPAF(int from,int to){
	
	while(!q.empty()) q.pop();

	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++) dis[i]=INF;
	
	dis[from]=0;
	q.push(from);
	vis[from]=true;

	
	while(!q.empty()){
		int now=q.front();
		q.pop();
		vis[now]=false;
		if(dis[now]==INF) continue;
		
		for(int i=head[now];i!=-1;i=edge[i].next){
	        int j=edge[i].to;
		if(dis[j]>dis[now]+edge[i].w ){
			dis[j]=dis[now]+edge[i].w;
			if(!vis[j]){
				vis[j]=true;
				q.push(j);
			}
		 }
	   }
	} 
	return dis[to];
}
int sovle(int x){
	int res=0;
	int num;
	for(int i=1;i<=n;i++){	
			num=SPAF(i,x)+SPAF(x,i);
			res=max(res,num);
	}
	return res;
}
int main(){
	int t,x;
	while(scanf("%d%d%d",&n,&m,&x)!=EOF){
		int a,b,c;
		cnt=0;
		memset(head,-1,sizeof(head));
		while(m--){
		   scanf("%d%d%d",&a,&b,&c);
		   Add(a,b,c);
		}
		int ans=sovle(x);
		cout<<ans<<endl;
	}
	return 0;
}

Perfect~  

猜你喜欢

转载自blog.csdn.net/qq_40922859/article/details/81676987