最小割+输出方案

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
const int INF=0x7FFFFFFF; struct Edge
{
int from,to,cap,flow;
};
vector<Edge>edges;
vector<int>G[maxn];
vector<int>CG[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int n,m,s,t;
Edge P[maxn]; //求出层次网络
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} //加边
void AddEdge(int from,int to,int cap)
{
Edge r;
r.from=from;
r.to=to;
r.cap=cap;
r.flow=;
edges.push_back(r);
Edge d;
d.from=to;
d.to=from;
d.cap=;
d.flow=;
edges.push_back(d);
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} //每个阶段来一次DFS增广
int DFS(int x,int a)
{
if(x==t||a==) return a;
int flow=,f;
for(int i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
} //多个阶段,多次建立层次网络。
int Maxflow(int ss,int tt)
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(ss,INF);
}
return flow;
} int N,M,Cost;
int U[maxn],V[maxn];
int FF[maxn]; void dfs(int x)
{
FF[x]=;
for(int i=; i<CG[x].size(); i++)
if(!FF[CG[x][i]])
dfs(CG[x][i]);
} int main()
{
while(~scanf("%d%d",&N,&M))
{
if(!N&&!M) break;
edges.clear();
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) CG[i].clear();
memset(FF,,sizeof FF);
s=;
t=;
for(int i=; i<=M; i++)
{
scanf("%d%d%d",&U[i],&V[i],&Cost);
AddEdge(U[i],V[i],Cost);
AddEdge(V[i],U[i],Cost);
}
Maxflow(s,t);
int Tot=;
for(int i=; i<edges.size(); i=i+)
{
P[Tot].from=edges[i].from;
P[Tot].to=edges[i].to;
P[Tot].cap=edges[i].cap;
P[Tot].flow=edges[i].flow;
Tot++;
}
for(int i=; i<Tot; i++)
{
if(P[i].cap-P[i].flow>)
CG[P[i].from].push_back(P[i].to);
if(P[i].flow>)
CG[P[i].to].push_back(P[i].from);
}
dfs(s);
for(int i=; i<=M; i++)
if((FF[U[i]]==&&FF[V[i]]==)||(FF[V[i]]==&&FF[U[i]]==))
printf("%d %d\n",U[i],V[i]);
printf("\n");
}
return ;
}

UVA 10480 Sabotage的更多相关文章

  1. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  2. UVA - 10480 Sabotage 最小割,输出割法

    UVA - 10480 Sabotage 题意:现在有n个城市,m条路,现在要把整个图分成2部分,编号1,2的城市分成在一部分中,拆开每条路都需要花费,现在问达成目标的花费最少要隔开那几条路. 题解: ...

  3. Uva 10480 Sabotage 最大流

    表示自从学了网络流,就基本上是一直用dinic 这个题一看就是用最大流,作为常识,两个点之间的最大流等于最小割 但是这个题需要输出割边,然后我就不会了,dinic判流量我觉得也可做,但是一直wa 然后 ...

  4. UVA - 10480 Sabotage【最小割最大流定理】

    题意: 把一个图分成两部分,要把点1和点2分开.隔断每条边都有一个花费,求最小花费的情况下,应该切断那些边.这题很明显是最小割,也就是最大流.把1当成源点,2当成汇点,问题是要求最小割应该隔断那条边. ...

  5. UVA - 10480 Sabotage (Dinic)

    The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebel-l ...

  6. UVA 10480 Sabotage (最大流最小割)

    题目链接:点击打开链接 题意:把一个图分成两部分,要把点1和点2分开.隔断每条边都有一个花费,求最小花费的情况下,应该切断那些边. 这题很明显是最小割,也就是最大流.把1当成源点,2当成汇点. 问题是 ...

  7. UVA 10480 Sabotage (最大流) 最小割边

    题目 题意: 编写一个程序,给定一个网络规范和破坏每个连接的成本,确定要切断哪个连接,以便将首都和最大的城市分离到尽可能低的成本. 分割-------------------------------- ...

  8. UVa 10480:Sabotage (最小割集)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. J - Sabotage - UVA 10480(最大流)

    题目大意:旧政府有一个很庞大的网络系统,可以很方便的指挥他的城市,起义军为了减少伤亡所以决定破坏他们的网络,使他们的首都(1号城市)和最大的城市(2号城市)不能联系,不过破坏不同的网络所花费的代价是不 ...

随机推荐

  1. jQuery的animate在火狐浏览器上不支持backgroundPositionX的解决方法

    在网上找的ffSupp.js文件 /** * 自定义backgroundPosition的animate,支持火狐,jQuery1.8以上版本 * @author Meleong * v1.00 */ ...

  2. lucene 自定义评分

    摘自:http://blog.csdn.net/seven_zhao/article/details/42708953 1.基于FunctionQuery,(1)创建类并继承ValueSource:( ...

  3. jfianl返回自定义的404页面

    public class MyErrorRenderFactory implements IErrorRenderFactory{ public Render getRender(int errorC ...

  4. linux和windows双系统时间错误解决方法

    转自http://www.2cto.com/os/201204/126212.html windows时间会慢8小时,原因: 两个概念: UTC即Universal Time Coordinated, ...

  5. 分布式日志收集系统Apache Flume的设计详细介绍

    问题导读: 1.Flume传输的数据的基本单位是是什么? 2.Event是什么,流向是怎么样的? 3.Source:完成对日志数据的收集,分成什么打入Channel中? 4.Channel的作用是什么 ...

  6. 《JS中的面向对象技术》

    内容要点: 1.什么是对象:JS权威指南学习总结-第六章 ,(有句话:一切都是对象) 2.什么面向对象 使用对象时,只关注对象提供的功能,不关注其内部细节,比如jQuery.面向对象是一种通用思想,并 ...

  7. JavaScript事件响应的基础语法总结

    1.onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用 //例子 <html> <head> ...

  8. 【Shell】Linux中分区脚本

    后天要中期答辩了,今天只刷了一个题,还没写出来,但是想更新博客,所以只有把之前写的东西贴出来了. 一个用于分区的shell脚本 1 #!/bin/bash #指定脚本解释器路径 2 echo &quo ...

  9. ggplot2 theme相关设置—文本调整

    在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...

  10. submit 防止重复提交 --禁止提交

    <form action="/apply/apply" method="POST" id="indentForm"> <p ...