POJ-3268-Silver Cow Party-(Swap-Map)

链接:http://poj.org/problem?id=3268

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

思路:扩展的Dijkstra算法+swap处理+取最大值

超时代码:2*V次调用Dijkstra函数.

#include<algorithm> 
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int MAX=1001,INF=0x3f3f3f3f; 
int cost[MAX][MAX];//cost[i][j]表示顶点i到顶点j的权值
int dis[MAX];
int d[MAX];//顶点s出发的最短路径
bool used[MAX];//已经访问过的点
int V;//顶点数 
bool cmp(int a,int b){
	return b<a;
}
void Dijkstra(int s){
	memset(d,INF,sizeof(d));//fill(d,d+V,INF);
	memset(used,false,sizeof(used));//fill(used,used+V,false);
	d[s]=0;
	for (int i=1;i<=V;i++){
		int mi=INF;
		for (int i=1;i<=V;i++){
			if (!used[i]&&d[i]<mi){
				mi=d[i];
				s=i;
			}
		}
		used[s]=1;
		for (int i=1;i<=V;i++){
			if (cost[s][i]!=INF){
				d[i]=min(d[i], d[s] + cost[s][i]);
			}
		}
	}
}
int main(){
	int n,m,x;
	cin>>n>>m>>x;
	int e1,e2,costA;
	memset(cost,INF,sizeof(cost));
	for(int i=0;i<=n;i++)
		cost[i][i]=0;		
	for(int i=0;i<m;i++){
		cin>>e1>>e2>>costA;
		cost[e1][e2]=costA; 
	} 
	V=n;
	int ans=0,ansMAX=0,j=0;
	for(int i=1;i<=n;i++){
		Dijkstra(x);
		ans=d[i];
		Dijkstra(i);
		dis[j++]=ans+d[x];
		//ansMAX=max(ansMAX,ans);
	}
	sort(dis,dis+j,cmp);
	cout<<dis[0]<<endl;
	//cout<<ansMAX<<endl;//有向图,不能够单纯的乘以2,考虑反向建图 
}

交换思路,反向建图.

    //扩展的Dijkstra算法,放在末尾
    for(int i=1;i<=V;i++)
        dis[i]+=d[i];//更新距离

#include<algorithm> 
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
/*void swap(int a,int b){
	int k=a;
	a=b;
	b=k;
}*/
using namespace std;
const int MAX=1001,INF=0x3f3f3f3f; 
int cost[MAX][MAX];//cost[i][j]表示顶点i到顶点j的权值
int d[MAX];//顶点s出发的最短路径
int dis[MAX]; 
bool used[MAX];//已经访问过的点
int V;//顶点数 
void Dijkstra(int s){
	memset(d,INF,sizeof(d));//fill(d,d+V,INF);
	memset(used,false,sizeof(used));//fill(used,used+V,false);
	d[s]=0;
	for (int i=1;i<=V;i++){
		int mi=INF;
		for (int i=1;i<=V;i++){
			if (!used[i]&&d[i]<mi){
				mi=d[i];
				s=i;
			}
		}
		used[s]=1;
		for (int i=1;i<=V;i++){
			if (cost[s][i]!=INF){
				d[i]=min(d[i], d[s] + cost[s][i]);
			}
		}
	}
	//扩展的Dijkstra算法
	for(int i=1;i<=V;i++)
		dis[i]+=d[i];//更新距离
	 
}
int main(){
	int n,m,x;
	cin>>n>>m>>x;
	int e1,e2,costA;
	memset(cost,INF,sizeof(cost));
	for(int i=0;i<=n;i++)
		cost[i][i]=0;		
	for(int i=0;i<m;i++){
		cin>>e1>>e2>>costA;
		cost[e1][e2]=costA; 
	} 
	V=n;
	Dijkstra(x);//先求出x到其他点
	//交换 
	for(int i=1;i<n;i++)
		for(int j=i+1;j<=n;j++)
			swap(cost[i][j],cost[j][i]); 
	Dijkstra(x);//反向求出到x的距离,实际上就是求反距离!
	int ansMAX=0;
	for(int i=1;i<=n;i++)
		ansMAX=max(ansMAX,dis[i]);
	cout<<ansMAX<<endl;
}
发布了73 篇原创文章 · 获赞 27 · 访问量 1236

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/103810453