题目链接: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: 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.
题目大意:n个nong农场,m条图,w村子;
s起点,e终点,l边权
都要去w村子,一个无向图,找出所有去和回来的最短路中的最长的那个最短路;
一开始拿到这道题,暴力mei每一个最短路,然后找到最长的
for(int i=1;i<=n;++i)
{
dijkstra(i);//i到其他点的最短路
len[i]=d[x];//i到x的最短路
}
dijkstra(x)//x到其他点的最短路
for(int i=1;i<=n;++i)
len[i]=len[i]+d[i];//x到i点的最短路
但是敲完后算了一下时间。。10^9。凉凉,肯定不行;
画了一下图,发现x到其他点反转一下就是其他点分别到x的最短路
于是只用两次dijkstra就能搞定了;
ac:反正没什么要求,直接复制粘贴,改个数,想优化的自己优化吧
#include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 水印
#define mod 1000000
//struct ac{
// int next,l;
//};
//
//vector<ac> biao[1100];
int shuzu[1100][1100];//正向图
int shuzu2[1100][1100];//反向图
int shuaxin[1100];
bool biaoji[1100];
int n,m,x;
void prime(int x)//第一个
{
clean(biaoji,0);
clean(shuaxin,INF);
shuaxin[x]=0;
for(int i=0;i<n;++i)
{
int can=-1;
int min=INF;
for(int j=1;j<=n;++j)
{
if(biaoji[j]==0&&shuaxin[j]<min)
{
can=j;
min=shuaxin[j];
}
}
if(can==-1)
break;
biaoji[can]=1;
for(int j=1;j<=n;++j)
{
// int num=biao[can][j].next;
// int l=biao[can][j].l;
// if(biaoji[num]==0&&shuaxin[num]>biao[can][j].l+shuaxin[can])
// shuaxin[num]=shuaxin[can]+biao[can][j].l;
if(biaoji[j]==0&&shuaxin[j]>shuzu[can][j]+shuaxin[can])
{
shuaxin[j]=shuzu[can][j]+shuaxin[can];
}
}
}
}
void prime2(int x)//第二个,复制的
{
clean(biaoji,0);
clean(shuaxin,INF);
shuaxin[x]=0;
for(int i=0;i<n;++i)
{
int can=-1;
int min=INF;
for(int j=1;j<=n;++j)
{
if(biaoji[j]==0&&shuaxin[j]<min)
{
can=j;
min=shuaxin[j];
}
}
if(can==-1)
break;
biaoji[can]=1;
for(int j=1;j<=n;++j)
{
// int num=biao[can][j].next;
// int l=biao[can][j].l;
// if(biaoji[num]==0&&shuaxin[num]>biao[can][j].l+shuaxin[can])
// shuaxin[num]=shuaxin[can]+biao[can][j].l;
if(biaoji[j]==0&&shuaxin[j]>shuzu2[can][j]+shuaxin[can])
{
shuaxin[j]=shuzu2[can][j]+shuaxin[can];
}
}
}
}
int main()
{
while(cin>>n>>m>>x)
{
// for(int i=0;i<1100;++i)
// biao[i].clear();
clean(shuzu,INF);
clean(shuzu2,INF);
for(int i=0;i<m;++i)
{
int s,e,l;
cin>>s>>e>>l;
// ac can;
// can.next=e;
// can.l=l;
// biao[s].push_back(can);
if(shuzu[s][e]>l)
shuzu[s][e]=l;
}
int len[1100]={0};
prime(x);
for(int i=1;i<=n;++i)
len[i]=shuaxin[i];
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
shuzu2[i][j]=shuzu[j][i];
}
prime2(x);
for(int i=1;i<=n;++i)
len[i]=len[i]+shuaxin[i];
int res=0;
for(int i=1;i<=n;++i)
res=res>len[i]?res:len[i];
cout<<res<<endl;
}
}
还有一个用spfa写的,优先队列优化:
要不然会超时。。
ac:
#include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 水印
#define mod 1000000
struct ac{
int b,l;
};
struct cmp{
bool operator()(const ac &a,const ac &b){
return a.l>b.l;//l小的优先
}
};
vector<ac> shuzu[1100];
//int shuzu[1100][1100];
int shuaxin[1100];
bool biaoji[1100];
int n,m,x;
void spfa(int s)
{
clean(shuaxin,INF);
clean(biaoji,0);
priority_queue<ac,vector<ac>,cmp> que;//优先队列优化
while(que.size())
que.pop();
//初始化
shuaxin[s]=0;
ac fir;
fir.b=s;
fir.l=0;
que.push(fir);
//起点入列
while(que.size())
{
ac now=que.top();//直接取最短的
que.pop();
int can=now.b;
if(shuaxin[can]<now.l||biaoji[can])
continue;
if(can==x)
return ;
//cout<<"1 ";
biaoji[can]=1;
//cout<<can<<" ";
for(int i=0;i<shuzu[can].size();++i)
{
ac e=shuzu[can][i];
//cout<<e.b<<" "<<e.l<<endl;
//e.l 即为len( can->e )
if(biaoji[e.b]==0&&shuaxin[e.b]>shuaxin[can]+e.l)
{//刷新 有可到达的点
shuaxin[e.b]=shuaxin[can]+e.l;
// cout<<shuaxin[e.b]<<" ";
e.l=shuaxin[e.b];
que.push(e);
}
}
}
}
void spfa2(int s)
{
clean(shuaxin,INF);
clean(biaoji,0);
priority_queue<ac,vector<ac>,cmp> que;
while(que.size())
que.pop();
//初始化
shuaxin[s]=0;
ac fir;
fir.b=x;
fir.l=0;
que.push(fir);
//起点入列
while(que.size())
{
ac now=que.top();
que.pop();
int can=now.b;
if(shuaxin[can]<now.l||biaoji[can])
continue;
biaoji[can]=1;
for(int i=0;i<shuzu[can].size();++i)
{
ac e=shuzu[can][i];
//e.l 即为len( can->e )
if(biaoji[e.b]==0&&shuaxin[e.b]>shuaxin[can]+e.l)
{//刷新
shuaxin[e.b]=shuaxin[can]+e.l;
e.l=shuaxin[e.b];
que.push(e);
}
}
}
}
int main()
{
cin>>n>>m>>x;
for(int i=0;i<m;++i)
{
int s,e,l;
cin>>s>>e>>l;
ac can;
can.b=e;
can.l=l;
shuzu[s].push_back(can);
}
int len[1100]={0};
for(int i=1;i<=n;++i)//遍历单源最短路
{
if(i==x)
continue;
spfa(i);
len[i]=len[i]+shuaxin[x];
//cout<<len[i]<<" ";
}
spfa2(x);//x的最短路
for(int i=1;i<=n;++i)
len[i]=len[i]+shuaxin[i];
int res=0;
for(int i=1;i<=n;++i)//遍历找最大的那个最短路
res=res>len[i]?res:len[i];
cout<<res<<endl;
}