题意:
在一个有向图中求n头牛从自己的起点走到x再从x走回来的最远距离
思路一开始是暴力跑dij……
讲道理不太可能……
然后就百度了一下 才知道把矩阵转置的话就只需要求两次x的单源最短路……
/* ***********************************************
Author :Sun Yuefeng
Created Time :2016/10/22 20:09:36
File Name :A.cpp
************************************************ */ #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<list>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e3+;
const int mod=1e7+;
int dx[]= {,,,-,,-,,-};
int dy[]= {,-,,,-,,,-}; int n,m,x,most;
int waycome[maxn][maxn];
int wayback[maxn][maxn];
int dist[maxn];
bool vis[maxn]; void dijkstra(int x,int way[][maxn]){
int dis[maxn];
for(int i=;i<=n;i++){
dis[i]=inf;
vis[i]=false;
}
dis[x]=;
for(int i=;i<=n;i++){
int k=-;
int min=inf;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<min){
min=dis[j];
k=j;
}
}
vis[k]=true;
for(int j=;j<=n;j++){
if(dis[j]>dis[k]+way[k][j]&&!vis[j])
dis[j]=dis[k]+way[k][j];
}
}
for(int i=;i<=n;i++){
dist[i]+=dis[i];
if(dist[i]>most) most=dist[i];
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d%d",&n,&m,&x)!=EOF){
M(waycome,inf);
M(wayback,inf);
M(dist,);
int u,v,w;
most=-;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
waycome[u][v]=w;
wayback[v][u]=w;
}
dijkstra(x,waycome);
dijkstra(x,wayback);
printf("%d\n",most);
}
return ;
}