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; }
相关文章推荐
- HDU 2066 一个人的旅行 (SPFA + 链式前向星)
- HDU-2066-一个人的旅行
- hdu 2066 一个人的旅行 (DIJ)
- hdu 2066 一个人的旅行
- HDU 2066 一个人的旅行 (裸的dij)
- hdu-2066 一个人的旅行(SPFA做法)
- HDU 2066 一个人的旅行【dijkstra】
- HDU 2066 【Dijkstra】 一个人的旅行
- hdu-2066-一个人的旅行(Dijkstra)
- hdu 2066 一个人的旅行(最短路问题)
- hdu 2066 一个人的旅行(最短路)
- hdu 2066 一个人的旅行(简单的Dijkstra)
- HDU 2066 一个人的旅行
- HDU - 2066- 一个人的旅行【最短路】
- hdu 2066 一个人的旅行(最短路)
- HDU 2066 一个人的旅行(Dijkstra)
- HDU 2066 一个人的旅行 (Dijkstra算法)
- hdu 2066 一个人的旅行
- HDU-#2066 一个人的旅行(Floyd & Dijkstra)
- HDU 2066 一个人的旅行