UVa 10986 Sending email (最短路+Dijkstra队列优化)
2015-08-06 11:04
603 查看
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<iostream> #include<queue> #include<vector> #include<set> #include<climits> #include<map> #include<string> using namespace std; int N,n,m,S,T; const int INF=1000000000; const int maxm=50000+10; const int maxn=20000+10; struct edge { int to,cost; edge(int to=0,int cost=0):to(to),cost(cost){} }; typedef pair<int,int> P; int d[maxn]; vector<edge> G[maxn]; void Dijkstra() { priority_queue<P,vector<P>,greater<P> > que; fill(d,d+n,INF); d[S]=0; que.push(P(0,S)); while(!que.empty()) { P p=que.top(); que.pop(); int v=p.second; if(d[v]<p.first)continue; if(v==T)break; for(int i=0;i<G[v].size();i++) { edge e=G[v][i]; if(d[e.to]>d[v]+e.cost) { d[e.to]=d[v]+e.cost; que.push(P(d[e.to],e.to)); } } } } int main() { int t=1; scanf("%d",&N); while(N--) { scanf("%d%d%d%d",&n,&m,&S,&T); for(int i=0;i<n;i++) { G[i].clear(); } for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); G[u].push_back(edge(v,w)); G[v].push_back(edge(u,w)); } Dijkstra(); if(d[T]==INF)printf("Case #%d: unreachable\n",t++); else printf("Case #%d: %d\n",t++,d[T]); } return 0; }
相关文章推荐
- poj3216Repairing Company 二分匹配之最小路径覆盖+floyd
- opencontrail本地编译定义标签
- Codeforces Round #Pi (Div. 2)567A Lineland Mail(模拟)
- tair分布式缓存
- 2015 Multi-University Training Contest 5 hdu 5348 MZL's endless loop
- http://blog.csdn.net/xiaanming/article/details/17483273
- Code Forces 567 A. Lineland Mail(水~)
- URAL 1031 Railway Tickets
- CodeForces 567A Lineland Mail 贪心
- 2015 HUAS Summer Trainning #4~C
- poj1273 Drainage Ditches(最大流)
- codeforces 538E E. Demiurges Play Again(博弈+树形dp)
- container-with-most-water
- 用行为树与状态机写AI
- Lineland Mail
- 2015 Multi-University Training Contest 6
- hdu 5348 MZL's endless loop 2015 Multi-University Training Contest 5
- CodeForces 567A-Lineland Mail
- A. Lineland Mail-水题-Codeforces Round #Pi (Div. 2)
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水