您的位置:首页 > 其它

hdu 2066 一个人的旅行 spfa + 建超级源点汇点

2014-12-01 19:41 183 查看
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 10005;
const int inf = 1 << 27;

struct node{
	int to, nxt, w;
}e[N*2];

int dis
;
int head
;
int T, S, D;
int maxx;
int tot;
int s
, t
;

void add(int u, int v, int w)
{
	e[tot].to = v;
	e[tot].w = w;
	e[tot].nxt = head[u];
	head[u] = tot++;
}

void spfa()
{
	queue<int> q;
	while( !q.empty() )
		q.pop();
	for( int i = 1; i <= maxx; i++ )
		dis[i] = inf;
	dis[0] = 0;
	q.push(0);
	while( !q.empty() )
	{
		int now = q.front();
		q.pop();
		for( int i = head[now]; ~i; i = e[i].nxt )
		{
			int to = e[i].to;
			if( dis[to] > dis[now] + e[i].w )
			{
				dis[to] = dis[now] + e[i].w;
				q.push(to);
			}
		}
	}
}

int main()
{
	while(~scanf("%d%d%d", &T, &S, &D))
	{
		memset(head, -1, sizeof(head));
		tot = 0;
		maxx = -1;
		int u, v, w;
		while(T--)
		{
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
			add(v, u, w);
			maxx = max(maxx, max(u, v));
		}
		for( int i = 1; i <= S; i++ )
		{
			scanf("%d", &s[i]);
			add(0, s[i], 1);
			add(s[i], 0, 1);
		}
		maxx ++;
		for( int i = 1; i <= D; i++ )
		{
			scanf("%d", &t[i]);
			add(maxx, t[i], 1);
			add(t[i], maxx, 1);
		}
		spfa();
		printf("%d\n", dis[maxx]-2);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: