sgu 252 Railway Communication
来源:互联网 发布:算法导论 第3版 pdf 编辑:程序博客网 时间:2024/06/05 05:01
题意:
就是给你一个带权的DAG,求把所有点都覆盖的路径最少有多少条,且同时保证权值最小,并输出路径方案!
思路:
DAG上的最小点覆盖,直接上二分图匹配就行了,但是要求权值最小,一种方法就是转化为费用流来求解,另外就是KM算法(可以来求解最大或最小权值)。
另外WA 6 的话,就是注意一下输出的先后顺序,也就会是找二分图中没有入边的点开始dfs输出(开始傻逼的加了个vis数组判重复访问就结果跪了1h QAQ)
代码:
#include <cstdio>#include <cstring>#include <queue>#define INF 0x3f3f3f3fusing namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> P;const int N = 210,M = 2800 ,MOD=7+1e9;struct edge{ int to,next,cap,flow,cost;}e[M];int uN,head[N],tot;int pre[N],dis[N];bool vis[N];void init(){ memset(head,-1,sizeof(head)); tot=0;}void addedge(int u,int v,int cap,int cost){ e[tot].to=v , e[tot].cap=cap , e[tot].cost=cost , e[tot].flow=0; e[tot].next=head[u] , head[u]=tot++; e[tot].to=u , e[tot].cap=0 , e[tot].cost=-cost , e[tot].flow=0; e[tot].next=head[v] , head[v]=tot++;}bool spfa(int s,int t){ queue<int> q; for(int i=0 ; i<uN ;i++) { dis[i]=INF , vis[i]=0 , pre[i]=-1; } dis[s] = 0; vis[s] = 1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i!=-1 ;i = e[i].next) { int v = e[i].to; if(e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].cost){ dis[v] = dis[u] + e[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = 1; q.push(v); } } } } return pre[t] != -1;}int MincostMaxflow(int s,int t,int &cost){ int flow = 0; cost = 0; while(spfa(s,t)) { int Min_f = INF; for(int i = pre[t];i != -1 ;i = pre[e[i^1].to]) { if(Min_f> e[i].cap-e[i].flow) Min_f = e[i].cap-e[i].flow; } for(int i = pre[t];i != -1 ;i = pre[e[i^1].to]) { e[i].flow += Min_f; e[i^1].flow -= Min_f; cost += Min_f*e[i].cost; } flow += Min_f; } return flow;}int path[N], cnt, n;int indu[N];bool tp;void dfs1(int v){ if(tp) path[cnt++] = v; for(int i = head[v];i != -1 ;i = e[i].next) { if(e[i].flow == 1) { int u = e[i].to - n; if(u >= 1 && u <= n) { indu[u] ++; dfs1(u); } } }}int main(){ int m; init(); scanf("%d%d",&n,&m); int S = 0, T = 2*n + 1; uN = T + 1; for(int i = 0;i < m;i ++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v+n,1,w); } for(int i = 1;i <= n;i ++) { addedge(S,i,1,0); addedge(i+n,T,1,0); } int cost = 0; int res = n - MincostMaxflow(S,T,cost); printf("%d %d\n", res, cost); tp = 0; for(int i = 1;i <= n;i ++) { dfs1(i); } tp = 1; for(int i = 1;i <= n;i ++) { if(indu[i] == 0) { cnt = 0; dfs1(i); printf("%d",cnt); for(int j = 0;j < cnt;j ++) { printf(" %d",path[j]); } printf("\n"); } } return 0;}
0 0
- sgu-252 Railway Communication
- sgu 252 Railway Communication
- 【SGU】252 Railway Communication 最小权路径覆盖
- sgu 252. Railway Communication (最小费用最大流)
- Railway Communication
- sgu252:Railway Communication(费用流)
- gu 252. Railway Communication (最小费用最大流) 输出方案
- Communication
- Communication
- Communication
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- (SGU
- SGU
- pcduino v3(ubuntu)右下角的wifi小扇形不见了(已解决)
- SwipeRefreshLayout:谷歌官方SDK包中的下拉刷新
- OC NSString类字符串拼接问题探究
- 我的第一篇博客:关于android的Camera在takePicture()时不回调onPictureTaken()方法
- FloatingActionButton
- sgu 252 Railway Communication
- requireJS入门二
- github开源社区代码学习目录
- Google Java编程风格指南中文版
- 批处理笔记
- 寻址方式 寄存器寻址方式 立即数寻址方式 存储器寻址方式: 直接寻址方式,寄存器间接寻址方式,变址和基址寻址方式,基址变址寻址方式 串操作寻址方式 I/O端口寻址方式
- 3Sum Closest
- php模拟post提交网易云信
- Eclipse中修改SVN用户名和密码方法