HDU5521(最短路+建边)
来源:互联网 发布:网络模块接法图解 b 编辑:程序博客网 时间:2024/05/25 18:10
图论太菜了啊= =。
题解:每个集合外多加一个点,使得集合中的点和这集合外的点连起来,权值为cost,反过来建一条权值为0的边,然后从1跑spfa,从n跑spfa,那最短路就是max(dist1[i],dist2[i])中的最小值。枚举一下就可以
其实挺水的
#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdio>#include <cmath>#include <map>#include <set>#include <queue>#include <vector>#define mod 1000000007#define INF 0x3f3f3f3f#define fuck() (cout << "----------------------------------------" << endl)using namespace std;const int maxn = 2000000 + 5;int dist1[maxn];int dist2[maxn];int queuenum[maxn];bool vis[maxn];int n,m;struct Edge{ int to; int cost; Edge(){} Edge(int To, int Cost) { to = To; cost = Cost; }};vector <Edge> edge[maxn];void init(){ for(int i=0; i<maxn; i++) edge[i].clear(); memset(dist1,INF,sizeof(dist1)); memset(dist2,INF,sizeof(dist2));}void addedge(int from, int to, int cost){ edge[from].push_back(Edge(to,cost));}void spfa1(int source){ memset(vis,false,sizeof(vis)); memset(queuenum,0,sizeof(queuenum)); dist1[source] = 0; queue < int > q; q.push(source); vis[source] = true; queuenum[source] = 1; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i=0; i<edge[u].size(); i++) { Edge eg = edge[u][i]; int to = eg.to; int cost = eg.cost; if(dist1[u] + cost < dist1[to]) { dist1[to] = dist1[u] + cost; if(!vis[to]) { q.push(to); queuenum[to]++; if(queuenum[to] >= n+m) return; vis[to] = true; } } } } return;}void spfa2(int source){ memset(vis,false,sizeof(vis)); memset(queuenum,0,sizeof(queuenum)); dist2[source] = 0; queue < int > q; q.push(source); vis[source] = true; queuenum[source] = 1; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i=0; i<edge[u].size(); i++) { Edge eg = edge[u][i]; int to = eg.to; int cost = eg.cost; if(dist2[u] + cost < dist2[to]) { dist2[to] = dist2[u] + cost; if(!vis[to]) { q.push(to); queuenum[to]++; if(queuenum[to] >= n+m) return; vis[to] = true; } } } } return;}int main(){ int T, kases = 1; scanf("%d",&T); while(T--) { init(); scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { int v,num,pt; scanf("%d%d",&v,&num); for(int j=0; j<num; j++) { scanf("%d",&pt); addedge(pt,i+n,v); addedge(i+n,pt,0); } } spfa1(1); spfa2(n); int maxcost = INF; for(int i=1; i<=n; i++) { int c = max(dist1[i],dist2[i]); if(c < maxcost) maxcost = c; } printf("Case #%d: ",kases++); if(maxcost == INF) { printf("Evil John\n"); continue; } printf("%d\n",maxcost); bool flag = true; for(int i=1; i<=n; i++) { int c = max(dist1[i],dist2[i]); if(c == maxcost) { if(flag) printf("%d",i); else printf(" %d",i); flag = false; } } printf("\n"); } return 0;}
阅读全文
0 0
- HDU5521(最短路+建边)
- hdu5521最短路(Dijkstra)
- HDU5521 Meeting (最短路)
- HDU5521(最短路)
- hdu5521 Meeting (最短路)
- hdu5521 Meeting最短路
- [hdu5521]最短路优化
- HDU5521 Meeting【最短路】
- HDU5521 最短路,添加点
- hdu5521 ICPC2015沈阳现场赛(最短路)
- ICPC2015(沈阳)HDU5521 建图技巧+最短路
- HDU5521:Meeting(最短路 & 集合点互达)
- HDU5521 双向最短路+优先队列优化
- hdu5521 spfa+超图转换+最短路+邻接表建图
- hdu5521-二维最短路,集合或者拆点
- HDU5521最短路径
- hdu5521 Meeting 最短路 2015ACM/ICPC亚洲区沈阳站现场赛
- HDU5521 Meeting([好题]最短路径)
- Python 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- 637. Average of Levels in Binary Tree
- Java开发.gitignore文件包含.iml,.log的看法
- 如何用git将项目代码上传到github
- jeesite 多数据源的使用
- HDU5521(最短路+建边)
- 赋值
- 关于职业规划和博客内容
- if语句使用举例
- 内联/嵌入/外部样式表的优先级
- OpenGL阴影,Shadow Volumes(附源程序,使用 VCGlib )
- 求两个数的最大公约数和最小公倍数
- python-025
- 2018-10月份-格科微电子 软件开发 笔试题目