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

如果那位大大刚AC完心情不错,且屋外天气晴朗就帮忙看看~~~

Posted by notwhile at 2009-08-16 11:14:48 on Problem 3463 and last updated at 2009-08-16 11:16:08
#define MAXN 1<<26
#include<iostream>
#include<algorithm>
using namespace std;

int i,j,k,n,m;
int A,B,C;
int map[1005][1005];
int dis[1005][2];
int v[1005];
int cnt[1005][2];
struct E
{
	E *next;
	int data;
	int len;
}*adj[1005],*p;

void Init()
{
	int i,j,k;
	for(i=1;i<=n;i++)
	{
		adj[i]=NULL;
		cnt[i][0]=cnt[i][1]=0;
		dis[i][0]=dis[i][1]=MAXN;
		v[i]=0;		
	}
}
void dij(int s){
	
	int i,j,Min,Mini,tp;
	E *p;
	v[s]=1;
	p=adj[s];
	while(p)
	{
		if(dis[p->data][0]>p->len)
		{
			dis[p->data][1]=dis[p->data][0];
			dis[p->data][0]=p->len;
			cnt[p->data][1]=cnt[p->data][0];
			cnt[p->data][0]=1;
		}
		else if(dis[p->data][0]==p->len)cnt[p->data][0]++;
		else
		{
			if(dis[p->data][1]>p->len)
			{
				dis[p->data][1]=p->len;
				cnt[p->data][1]=1;
			}
			else if(dis[p->data][1]==p->len)
				cnt[p->data][1]++;
		}
		p=p->next;
	}
	dis[s][0]=dis[s][1]=0;
	for(i=1;i<n;i++)
	{
		Min=MAXN,Mini=-1;
		for(j=1;j<=n;j++)
			if(!v[j] && Min>dis[j][0])
				Min=dis[j][0],Mini=j;
		if(Mini<0)break;
		v[Mini]=1;
		for(p=adj[Mini];p;p=p->next){
			j=p->data;
			if(dis[j][0]>Min+p->len)
			{
				dis[j][1]=dis[j][0];
				dis[j][0]=Min+p->len;
				cnt[j][1]=cnt[j][0];
				cnt[j][0]=cnt[Mini][0];
			}
			else if(dis[j][0]==Min+p->len)
				 cnt[j][0]++;
			else 
			{
				tp=Min+p->len;
				if(dis[j][1]>tp)
					dis[j][1]=tp,cnt[j][1]=cnt[Mini][1];
				else if(dis[j][1]==tp)
					cnt[j][1]++;
			}
		}
	}	
}

int main()
{
	int T,s,e,sum;
	for(scanf("%d",&T);T--;)
	{
		scanf("%d%d",&n,&m);
		Init();
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&A,&B,&C);
			p=new E;
			p->data=B;
			p->len=C;
			p->next=adj[A];
			adj[A]=p;
		}
		scanf("%d%d",&s,&e);
		dij(s);
		sum=0;
		if(dis[e][0]<MAXN)sum+=cnt[e][0];
		if(dis[e][0]==dis[e][1]-1)sum+=cnt[e][1];
		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