Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求 d[x][i]和d[i][x]也可以过,只不过慢一点。

Posted by 1094618304 at 2015-05-14 11:10:38 on Problem 3268
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator