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

开到200000也re,真不知道哪里越界了

Posted by 915507430 at 2014-09-09 09:22:10 on Problem 3013
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=200000;
const __int64 inf =9999999999LL;
struct edge
{
	int from,to,w,next;
}e[200000];
int head[maxn];
int vis[maxn];
__int64 dist[maxn];
int point[maxn];
int n,m,t;
void add(int i,int j,int w)
{
	e[t].from=i;
	e[t].to=j;
	e[t].w=w;
	e[t].next=head[i];
	head[i]=t++;
}
void spfa(int s)
{
	queue <int> q;
	for(int i=1;i<=n;i++)
		dist[i]=inf;
	memset(vis,false,sizeof(vis));
	q.push(s);
	dist[s]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=false;
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			int v=e[i].to;
			if(dist[v]>dist[u]+e[i].w)
			{
				dist[v]=dist[u]+e[i].w;
				if(!vis[v])
				{
					vis[v]=true;
					q.push(v);
				}
			}
		}
	}
}
int main()
{
	int a,b,c,T;
	while(scanf("%d",&T))
	{
		while(T--)
		{
			scanf("%d%d",&n,&m);
			t=0;
			memset(head,-1,sizeof(head));
			memset(point,0,sizeof(point));
			memset(e,0,sizeof(e));
			for(int i=1;i<=n;i++)
				scanf("%d",&point[i]);
			while(m--)
			{
				scanf("%d%d%d",&a,&b,&c);
				add(a,b,c);
				add(b,a,c);
			}
			spfa(1);
			int flag=1;
			__int64 sum=0;
			for(int i=2;i<=n;i++)
			{
				if(dist[i]==inf)
					flag=0;
				else
					sum+=dist[i]*point[i];
			}
			if(flag==0)
				printf("No Answer\n");
			else
				printf("%I64d\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