ZOJ 2504 Help John! 【最短路】 【Dijkstra】

 
//2632155 	2011-08-18 21:18:49 	Accepted            2504 	C++ 	 30 	 908 	ylwh@Unknown
//2627388 	2011-08-14 20:08:17 	Wrong Answer 	    2504 	C++ 	 30 	4308 	ylwh@Unknown
//2627355 	2011-08-14 19:48:41 	Wrong Answer 	    2504 	C++ 	 30 	4308 	ylwh@Unknown
//2627276 	2011-08-14 18:37:24 	Wrong Answer 	    2504 	C++ 	 30 	4116 	ylwh@Unknown
//2627271 	2011-08-14 18:34:20 	Wrong Answer 	    2504 	C++ 	110 	4116 	ylwh@Unknown
//2626370 	2011-08-14 10:07:49 	Wrong Answer 	    2504 	C++ 	  0 	1000 	ylwh@Unknown
//2626350 	2011-08-14 09:55:10 	Wrong Answer 	    2504 	C++ 	  0 	1000 	ylwh@Unknown
//2626345 	2011-08-14 09:52:46 	Wrong Answer 	    2504 	C++ 	10 	    1000 	ylwh@Unknown
//2626343 	2011-08-14 09:51:42 	Compilation Error 	2504 	C++ 	 0 	       0 	ylwh@Unknown
//2626286 	2011-08-14 08:56:20 	Wrong Answer 	    2504 	C++ 	 0 	    1000 	ylwh@Unknown
//2626281 	2011-08-14 08:38:35 	Wrong Answer 	    2504 	C++ 	 0 	    1000 	ylwh@Unknown
//2626276 	2011-08-14 08:19:42 	Wrong Answer 	    2504 	C++      0 	   11900 	ylwh@Unknown
//2626136 	2011-08-13 21:30:32 	Segmentation Fault 	2504 	C++ 	 0 	       0 	ylwh@Unknown
//2626135 	2011-08-13 21:30:02 	Segmentation Fault 	2504 	C++ 	 0 	       0 	ylwh@Unknown
//2626133 	2011-08-13 21:29:34 	Segmentation Fault 	2504 	C++ 	 0 	       0 	ylwh@Unknown
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define M 30001
#define N 1001
#define MAX 20000000
struct edge
{
	int x, len;
	struct edge * next;
}e[M*2+N];
int d[N], staue[N], tmd[N];
int main()
{
	int t, tt, n, m, i, j, k, cnt;
	struct edge * next;
	scanf("%d", &t);
	for(tt=1; tt<=t; tt++)
	{
		if(scanf("%d%d", &n, &m) == EOF)
			break;
		cnt = n;
		for(i=1; i<=n; i++)
		{
			d[i] = MAX;
			e[i].next = NULL;
			staue[i] = 0;
		}
		int a, b, c;
		while(m--)
		{
			scanf("%d%d%d", &a, &b, &c);

			e[++cnt].x = a;
			e[  cnt].len = c;
			e[  cnt].next = e[b].next;
			e[b].next = e + cnt;
			e[++cnt].x = b;
			e[  cnt].len = c;
			e[  cnt].next = e[a].next;
			e[a].next = e + cnt;
		}
		int kstaue;
		int flag = 0;
		d[1] = 0;
		scanf("%d", &k);
		scanf("%d", &tmd[1]);
		for(i=2; i<=k; i++)
		{
			kstaue = 0;
			scanf("%d", &tmd[i]);
			next = e[ tmd[i-1] ].next;
			while(next != NULL)
			{
				if(next->x == tmd[i])
				{
					kstaue = 1;
					d[ tmd[i] ] = d[ tmd[i-1] ] + next->len;
					break;
				}
				next = next->next;
			}
			if( !kstaue )
				flag = 1;
		}
		if(flag || d[n] == MAX || tmd[1] != 1 || tmd[k] != n)
		{
			printf("TEST %d N\n", tt);
			continue;
		}
		int time1 = d[n];
		staue[1] = staue[ tmd[2] ] = 1;
		int now = tmd[2];
		int mins;
		for(i=3; i<=n; i++)
		{
			next = e[now].next;
			while(next != NULL)
			{
				if( !staue[next->x] && d[ next->x ] > d[now] + next->len)
					d[ next->x ] = d[now] + next->len;
				next = next->next;
			}
			mins = MAX;
			for(j=1; j<=n; j++)
				if( !staue[j] && d[j] < mins)
				{
					now = j;
					mins = d[j];
				}
			staue[now] = 1;
		}
		int time2 = d[n];
		printf("TEST %d Y %d\n", tt, time1 - time2);
	}
    return 0;
}


你可能感兴趣的:(ZOJ 2504 Help John! 【最短路】 【Dijkstra】)