Meeting HDU - 5521 (单源最短路)
2017-08-30 20:48
204 查看
题意:
给你n个点,然后有m个城市群,城市群之间相互可以在ti时间内到达,现在问两个人从1和n同时出发,最少需要花费多少时间相遇,然后输出可能的相遇地点。思路:
首先是间城市群转化为有向图,每个城市群增加一个点,从每个城市有一条出边和一条入边,权值是ti/2(实际代码实现的时候为了避免浮点数,都乘了2),然后这个用少量的边就满足了题目的城市群互达的条件了。然后就从1和n做一遍单源最短路就好了,对它们到达某个点的时间a[i]和b[i]取最大值,这个值就是两人在这里相遇所需要的时间,然后找个最小的就是答案了,再把方案输出就好了。
写的时候数组的大小名写错了,wa了一发,好像这种错误不是一次两次了,警记交之前看一遍
代码:
#define debug printf #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<map> #include<vector> #include<queue> using namespace std; typedef long long LL; const int maxn=1e5+100; const int maxm=3e6+100; int T; int n,m; int kase=0; struct Edge{ int v,w,next; Edge(){} Edge(int v,int w,int next) :v(v),w(w),next(next){} }; const int maxnode=1e6+maxn; Edge edges[maxm]; int head[maxnode]; int edgescnt; LL times[maxnode],timed[maxnode]; bool viss[maxnode],visd[maxnode]; const LL INF=1e18; void initEdge(); void AddEdge(int u,int v,int w); void Dijkstra(int s,LL * d,int n,bool done[]); void solve(); int main() { scanf("%d",&T); kase=0; while(T--){ kase++; solve(); } return 0; } void initEdge() { memset(head,-1,sizeof(head)); edgescnt=0; } void AddEdge(int u,int v,int w) { edges[edgescnt]=Edge(v,w,head[u]); head[u]=edgescnt++; } void solve() { scanf("%d %d",&n,&m); initEdge(); int time,sz,u; for(int i=1;i<=m;i++){ scanf("%d %d",&time,&sz); for(int j=1;j<=sz;j++){ scanf("%d",&u); AddEdge(u,i+n,time); AddEdge(i+n,u,time); } } Dijkstra(1,times,n+m,viss); Dijkstra(n,timed,n+m,visd); // for(int i=1;i<=n+m;i++){ // debug("%d %lld %lld\n",i,times[i],timed[i]); // } LL ans=INF; LL tmpmin=INF; for(int i=1;i<=n;i++){ tmpmin=max(times[i],timed[i]); if(tmpmin<ans){ ans=tmpmin; } } if(ans==INF){ printf("Case #%d: Evil John\n",kase); return; } vector<int> res; for(int i=1;i<=n;i++){ tmpmin=max(times[i],timed[i]); if(tmpmin==ans){ res.push_back(i); } } printf("Case #%d: %lld\n",kase,ans/2); for(int i=0;i<res.size();i++){ if(i!=0) printf(" "); printf("%d",res[i]); } printf("\n"); } struct HeapNode{ LL d; int u; HeapNode(){} HeapNode(LL d,int u) :d(d),u(u){} bool operator < (const HeapNode & rhs) const { return d>rhs.d; } }; void Dijkstra(int s,LL * d,int n,bool done[]) { priority_queue<HeapNode> Q; for(int i=1;i<=n;i++) d[i]=INF; d[s]=0; for(int i=1;i<=n;i++) done[i]=false; Q.push(HeapNode(0,s)); while(!Q.empty()){ HeapNode x=Q.top();Q.pop(); int u=x.u; if(done[u]) continue; done[u]=true; for(int e=head[u];e!=-1;e=edges[e].next){ int v=edges[e].v,w=edges[e].w; if(d[v]>d[u]+LL(w)){ d[v]=d[u]+LL(w); Q.push(HeapNode(d[v],v)); } } } }
相关文章推荐
- HDU 3790 最短路径问题(单源最短路)
- Sicily 1823 Hardest Job Ever!(单源最短路)
- [hiho 23]单源最短路-Dijstra算法
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- POJ - 1797 Heavy Transportation 单源最短路
- uva 10557 - XYZZY 关键词:正环,无敌bfs,单源最短路spfa算法
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- 求单源最短路的SPFA算法
- HDU 5521 [ACM 2015 沈阳] Meeting [最短路]
- HDU 1535 Invitation Cards 单源最短路
- hdoj3339In Action【单源最短路+01背包】
- 【算法】单源最短路——SPFA
- POJ 3268 Silver Cow Party 正反单源最短路
- HDU 5637 Transform 单源最短路
- hdu 2066 一个人的旅行(单源最短路dijkstra)and hdu 2544
- HDU 5521 Meeting 化简边的最短路
- 单源最短路Dijkstra算法——matlab实现
- hdu 5521 最短路
- 【单源最短路】dijstra poj 1502
- HDU 2544 最短路 (单源最短路)