UVa11248 Frequency Hopping(最大流+最小割)
来源:互联网 发布:mac 命令行 文件夹 编辑:程序博客网 时间:2024/06/03 16:16
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206
【思路】
最大流最小割。
可以确定的是如果不可行需要修改的是流量已经达到上限的最小割中的边。可以考虑依次修改求最大流。
优化:1 在原最大流的基础上增广; 2 只增广到流量C为止。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxn = 100+10; 9 const int INF = 1e9*2+10; 10 11 struct Edge{ 12 int u,v,cap,flow; 13 bool operator<(const Edge& rhs) const{ 14 return u<rhs.u || (u==rhs.u && v<rhs.v) ; 15 } 16 }; 17 struct Dinic { 18 int n,m,s,t; 19 bool vis[maxn]; 20 int d[maxn],cur[maxn]; 21 vector<int> G[maxn]; 22 vector<Edge> es; 23 24 void init(int n) { 25 this->n=n; 26 es.clear(); 27 for(int i=0;i<n;i++) G[i].clear(); 28 } 29 void clearflow() { 30 for(int i=0;i<es.size();i++) es[i].flow=0; 31 } 32 void AddEdge(int u,int v,int cap) { 33 es.push_back((Edge){u,v,cap,0}); 34 es.push_back((Edge){v,u,0,0}); 35 m=es.size(); 36 G[u].push_back(m-2); 37 G[v].push_back(m-1); 38 } 39 40 bool BFS() { 41 queue<int> q; 42 memset(vis,0,sizeof(vis)); 43 q.push(s); vis[s]=1; d[s]=0; 44 while(!q.empty()) { 45 int u=q.front(); q.pop(); 46 for(int i=0;i<G[u].size();i++) { 47 Edge& e=es[G[u][i]]; 48 int v=e.v; 49 if(!vis[v] && e.cap>e.flow) { 50 vis[v]=1; 51 d[v]=d[u]+1; 52 q.push(v); 53 } 54 } 55 } 56 return vis[t]; 57 } 58 int DFS(int u,int a) { 59 if(u==t || a==0) return a; 60 int flow=0,f; 61 for(int& i=cur[u];i<G[u].size();i++){ 62 Edge& e=es[G[u][i]]; 63 int v=e.v; 64 if( d[v]==d[u]+1 && (f=DFS(v,min(a,e.cap-e.flow)))>0 ) { 65 e.flow+=f; 66 es[G[u][i]^1].flow-=f; 67 flow+=f,a-=f; 68 if(!a) break; 69 } 70 } 71 return flow; 72 } 73 int Maxflow(int s,int t,int limit) { 74 this->s=s , this->t=t; 75 int flow=0; 76 while(BFS() && flow<limit) { 77 memset(cur,0,sizeof(cur)); 78 flow+=DFS(s,INF); 79 } 80 return flow; 81 } 82 vector<int> Mincut() { 83 vector<int> ans; 84 for(int i=0;i<es.size();i++) 85 if(es[i].cap!=0 && es[i].cap==es[i].flow) 86 ans.push_back(i); 87 return ans; 88 } 89 void reduce() { 90 for(int i=0;i<es.size();i++) es[i].cap-=es[i].flow; 91 } 92 }dc; 93 94 int n,m,C; 95 96 int main() { 97 int kase=0; 98 while(scanf("%d%d%d",&n,&m,&C)==3 && n) { 99 dc.init(n);100 int u,v,w;101 for(int i=0;i<m;i++) {102 scanf("%d%d%d",&u,&v,&w);103 u--,v--;104 dc.AddEdge(u,v,w);105 }106 int flow=dc.Maxflow(0,n-1,INF);107 printf("Case %d: ",++kase);108 if(flow>=C) printf("possible\n");109 else {110 vector<int> cut;111 vector<Edge> ans;112 cut=dc.Mincut();113 dc.reduce(); //考虑除去最大流之后的剩余网络//即在原流基础上增广 114 for(int i=0;i<cut.size();i++) {115 dc.clearflow();116 Edge& e=dc.es[cut[i]];117 int tmp=e.cap;118 e.cap=C;119 if(flow+dc.Maxflow(0,n-1,C-flow)>=C) ans.push_back(e);120 e.cap=tmp;121 }122 if(!ans.size()) printf("not possible\n");123 else {124 sort(ans.begin(),ans.end());125 printf("possible option:");126 int d=ans.size();127 for(int i=0;i<d-1;i++) printf("(%d,%d),",ans[i].u+1,ans[i].v+1); 128 printf("(%d,%d)\n",ans[d-1].u+1,ans[d-1].v+1);129 }130 }131 }132 return 0;133 }
0 0
- UVa11248 Frequency Hopping(最大流+最小割)
- UVA 11248 Frequency Hopping(最大流、最小割)
- Uva 11248Frequency Hopping 网络流 最大流最小割
- UVa 11248 - Frequency Hopping - 最大流 - 最小割
- UVA 11248 Frequency Hopping (最大流+最小割)
- uva11248 Frequency Hopping
- UVa11248 - Frequency Hopping
- UVA11248【最大流最小割入门】
- uva 11248 - Frequency Hopping 最大流最小割入门题 求割集模板
- 【最小割】 UVA 11248 Frequency Hopping
- UVA 11248 Frequency Hopping 最小割
- uva 11248 Frequency Hopping (最大流)
- Uva 11248 Frequency Hopping(最大流)
- Uva 11248 Frequency Hopping (最大流+残余网络)
- UVa 11248 - Frequency Hopping 最大流
- UVA 11248 Frequency Hopping (最大流)
- UVA - 11248 Frequency Hopping(网络流+割)
- UVA 11248 - Frequency Hopping(网络流)
- 1022D进制的A+B
- uoj#67. 新年的毒瘤(割顶)
- dp入门
- JPEG图像压缩算法流程详解
- UVa10779 Collectors Problem(最大流)
- UVa11248 Frequency Hopping(最大流+最小割)
- UVAlive2531 The K-League(最大流)
- UVa1658 Admiral(拆点法+最小费用流)
- 跟踪程序异常退出
- 第五周项目四-长方柱型(2)
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
- IOS 字体抛光动画
- JAVA中,static和三个权限关键字的总结
- tyvj P1517 飘飘乎居士的乌龟(最大流)