在一个农场里面所有的牛都会来参加大牛举办的派对,不过农场的路都是单向的,而且每头牛都喜欢都最短的路程,那么问题来了,求出来来回花费时间最多的那头牛所用的时间。。。
/////////////////////////////////////////////////////////
从派对的地点求一遍spfa可以得到回去路的最短距离,如果添加反边就是去的路的最短距离,淡然注意分开.....
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
const int maxn = 1005;
const int oo = 0xfffffff;
struct node
{
int y, time;
node(int y, int time):y(y), time(time){}
};
vector<node>gLeave[maxn];
vector<node>gBack[maxn];
int vLeave[maxn], vBack[maxn];
void spfa(vector<node> G[], int s, int v[])
{
queue<int> Q;
Q.push(s);
while(Q.size())
{
s = Q.front(), Q.pop();
int len = G[s].size();
for(int i=0; i<len; i++)
{
node q = G[s][i];
if(v[q.y] > v[s]+q.time)
{
v[q.y] = v[s]+q.time;
Q.push(q.y);
}
}
}
}
int main()
{
int N, M, S;
while(scanf("%d%d%d", &N, &M, &S) != EOF)
{
int i, a, b, t;
for(i=1; i<=N; i++)
{
vLeave[i] = vBack[i] = oo;
gLeave[i].clear();
gBack[i].clear();
}
vLeave[S] = vBack[S] = 0;
for(i=0; i<M; i++)
{
scanf("%d%d%d", &a, &b, &t);
gLeave[b].push_back(node(a, t));
gBack[a].push_back(node(b, t));
}
spfa(gLeave, S, vLeave);
spfa(gBack, S, vBack);
int ans = 0;
for(i=1; i<=N; i++)
ans = max(ans, vLeave[i]+vBack[i]);
printf("%d ", ans);
}
return 0;
}
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
const int maxn = 1005;
const int oo = 0xfffffff;
struct node
{
int y, time;
node(int y, int time):y(y), time(time){}
};
vector<node>gLeave[maxn];
vector<node>gBack[maxn];
int vLeave[maxn], vBack[maxn];
void spfa(vector<node> G[], int s, int v[])
{
queue<int> Q;
Q.push(s);
while(Q.size())
{
s = Q.front(), Q.pop();
int len = G[s].size();
for(int i=0; i<len; i++)
{
node q = G[s][i];
if(v[q.y] > v[s]+q.time)
{
v[q.y] = v[s]+q.time;
Q.push(q.y);
}
}
}
}
int main()
{
int N, M, S;
while(scanf("%d%d%d", &N, &M, &S) != EOF)
{
int i, a, b, t;
for(i=1; i<=N; i++)
{
vLeave[i] = vBack[i] = oo;
gLeave[i].clear();
gBack[i].clear();
}
vLeave[S] = vBack[S] = 0;
for(i=0; i<M; i++)
{
scanf("%d%d%d", &a, &b, &t);
gLeave[b].push_back(node(a, t));
gBack[a].push_back(node(b, t));
}
spfa(gLeave, S, vLeave);
spfa(gBack, S, vBack);
int ans = 0;
for(i=1; i<=N; i++)
ans = max(ans, vLeave[i]+vBack[i]);
printf("%d ", ans);
}
return 0;
}