| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求 d[x][i]和d[i][x]也可以过,只不过慢一点。#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn= 1010;
const int INF = 1<<29;
struct edge{
int to,cost;
edge(){}
edge(int x,int y) {
to=x;
cost=y;
}
};
typedef pair<int,int>P;
int N,M,X;
vector<edge>G[maxn];
int d[maxn];
int dijkstra(int s,int t) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=0;i<=N;i++) d[i]=INF;
d[s]=0;
que.push(P(0,s));
while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(v==t) return d[t];
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b,c;
while(~scanf("%d%d%d",&N,&M,&X)) {
for(int i=0;i<M;i++) {
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(edge(b,c));
}
int sum=0;
for(int i=1;i<=N;i++) {
sum=max(sum,dijkstra(i,X)+dijkstra(X,i));
}
printf("%d\n",sum);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator