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

求指正bug,菇凉无以为报

Posted by ninesun127 at 2017-09-17 20:33:53 on Problem 3268
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<utility>
#include<cstdio>
using namespace std;
typedef pair<int,int>pr;

const int maxn=1100;
int n,m,s;
int dis[maxn],tmp[maxn];
struct edge{
	int u,v,w;
};
vector<edge>edges;
vector<int>g[maxn];
int book[maxn];
void insert(int u,int v,int w)
{
	edges.push_back(edge{u,v,w}),g[u].push_back(edges.size()-1);
	
}
void dijkstra()
{
	memset(book,0,sizeof(book));
	memset(dis,0x3f,sizeof(dis));

	priority_queue<pr,vector<pr>,greater<pr> >q;
	q.push(make_pair(0,s));dis[s]=0;
	
	while(!q.empty())
	{
		pr tt=q.top();q.pop();
		
		int x=tt.second;
		if(book[x])continue;book[x]=1;
		for(int i=0;i<g[x].size();i++)
		{
			edge e=edges[g[x][i]];
			if(dis[e.v]>dis[x]+e.w)
			{
				dis[e.v]=dis[x]+e.w;
				q.push(make_pair(dis[e.v],e.v));
			}
		}
	}
	
	
}
int X[maxn],Y[maxn],Z[maxn];

void init()
{
	edges.clear();
	for(int i=1;i<=n;i++)g[i].clear();
}
int main()
{
	while(~scanf("%d %d %d",&n,&m,&s))
	{
		init();
		for(int i=1;i<=m;i++)
		{
			int x,y,z;scanf("%d %d %d",&x,&y,&z);
			X[i]=x,Y[i]=y,Z[i]=z;
			insert(x,y,z);
		}
		dijkstra();
		memcpy(tmp,dis,sizeof(dis));
	
		init();
		for(int i=1;i<=m;i++)
		{
			insert(Y[i],X[i],Z[i]);
		}
		dijkstra();
		int ans=0;int index;
		for(int i=1;i<=n;i++)
		{	
			if(tmp[i]+dis[i]>ans)
				ans=max(ans,tmp[i]+dis[i]),index=i;
		}
	//cout<<index<<" "<<tmp[index]<<" "<<dis[index]<<endl; 
		printf("%d\n",ans);
	}
	
	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