UVA10480:Sabotage(最小割+输出)
Sabotage
题目链接:https://vjudge.net/problem/UVA-10480
Description:
The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebellion. Because of the enormous disturbances this is causing in world economy, an imperialist military super power has decided to invade the country and reinstall the old regime. For this operation to be successful, communication between the capital and the largest city must be completely cut. This is a difficult task, since all cities in the country are connected by a computer network using the Internet Protocol, which allows messages to take any path through the network. Because of this, the network must be completely split in two parts, with the capital in one part and the largest city in the other, and with no connections between the parts. There are large differences in the costs of sabotaging different connections, since some are much more easy to get to than others. Write a program that, given a network specification and the costs of sabotaging each connection, determines which connections to cut in order to separate the capital and the largest city to the lowest possible cost.
Input:
Input file contains several sets of input. The description of each set is given below. The first line of each set has two integers, separated by a space: First one the number of cities, n in the network, which is at most 50.
The second one is the total number of connections, m, at most 500. The following m lines specify the connections. Each line has three parts separated by spaces: The first two are the cities tied together by that connection (numbers in the range 1 − n).
Then follows the cost of cutting the connection (an integer in the range 1 to 40000000). Each pair of cites can appear at most once in this list. Input is terminated by a case where values of n and m are zero. This case should not be processed. For every input set the capital is city number 1, and the largest city is number 2.
Output:
For each set of input you should produce several lines of output. The description of output for each set of input is given below: The output for each set should be the pairs of cities (i.e. numbers) between which the connection should be cut (in any order), each pair on one line with the numbers separated by a space. If there is more than one solution, any one of them will do. Print a blank line after the output for each set of input.
Sample Input:
5 8 1 4 30 1 3 70 5 3 20 4 3 5 4 5 15 5 2 10 3 2 25 2 4 50
5 8 1 4 30 1 3 70 5 3 20 4 3 5 4 5 15 5 2 10 3 2 25 2 4 50
0 0
Sample Output:
4 1 3 4 3 5 3 2
4 1 3 4 3 5 3 2
题意:
给出起点1和终点2,并且给出一些边的边权,现在要截断一些边使1和2彻底隔离。问怎样截断花费最小。
题解:
就是一个最小割,起点1在S集,终点2在T集。
最后输出的时候注意一下就好了,从起点1出发,如果遇到容量为0的边那么这条边就是一条割了。注意并不是所有容量为0的边都是割。
这里我有个问题,为什么在最后dfs的时候输出结果不行,记录一下再输出就可以A...希望大佬不吝赐教。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 100000000
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = ,M = ;
int head[N],d[N];
const int s = ,t = ;
int tot ;
struct Edge{
int u,v,next,c;
}e[M<<];
void adde(int u,int v,int c){
e[tot].u=u;e[tot].v=v;e[tot].c=c;e[tot].next=head[u];head[u]=tot++;
e[tot].u=v;e[tot].v=u;e[tot].c=c;e[tot].next=head[v];head[v]=tot++;
}
int n,m;
int bfs(){
memset(d,,sizeof(d));d[]=;
queue <int > q;q.push();
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> && !d[v]){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[t]!=;
}
ll dfs(int u,int a){
if(u==t || a==) return a;
ll flow = ,f;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[u]+) continue ;
f=dfs(v,min(e[i].c,a));
if(f>){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break ;
}
}
if(!flow) d[u]=-;
return flow ;
}
void Dinic(){
ll tmp=;
while(bfs()) tmp+=dfs(,INF);
return ;
}
int vis[N];
void Go(int u){
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(vis[v]) continue ;
if(e[i].c>) Go(v);
if(e[i].c==) printf("%d %d\n",u,v); //这样输出不行
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(!n && !m) break ;
tot=;memset(head,-,sizeof(head));
int cnt = ;
for(int i=;i<=m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
adde(u,v,c);
}
Dinic();
memset(vis,,sizeof(vis));
Go();
/*for(int i=0;i<tot;i+=2){ //
int u=e[i].u,v=e[i].v;
if((vis[u]&&!vis[v]) || (!vis[u]&&vis[v])) printf("%d %d\n",u,v);
}*/
printf("\n");
}
return ;
}
UVA10480:Sabotage(最小割+输出)的更多相关文章
- UVA10480 Sabotage —— 最小割最大流
题目链接:https://vjudge.net/problem/UVA-10480 题解: 实际就是求最小割集. 1.什么是网络流图的“割”?答:一个边的集合,使得网络流图删除这些边之后,点被分成两部 ...
- UVA - 10480 Sabotage 最小割,输出割法
UVA - 10480 Sabotage 题意:现在有n个城市,m条路,现在要把整个图分成2部分,编号1,2的城市分成在一部分中,拆开每条路都需要花费,现在问达成目标的花费最少要隔开那几条路. 题解: ...
- uva10480(最小割)
传送门:Sabotage 题意:给定多个城市的网络,每个城市之间的通信有花费,要求使得首都和最大城市之间的通信断掉的最小花费.要求输出任意一组砸掉的边. 分析:跑一遍最大流dinic后,根据最小割定理 ...
- HDU 3251 Being a Hero(最小割+输出割边)
Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...
- 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)
https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...
- UVA 10480 Sabotage (网络流,最大流,最小割)
UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...
- POJ 2125 Destroying The Graph (二分图最小点权覆盖集+输出最小割方案)
题意 有一个图, 两种操作,一种是删除某点的所有出边,一种是删除某点的所有入边,各个点的不同操作分别有一个花费,现在我们想把这个图的边都删除掉,需要的最小花费是多少. 思路 很明显的二分图最小点权覆盖 ...
- Expm 10_2 实现Ford-Fulkerson算法,求出给定图中从源点s到汇点t的最大流,并输出最小割。
package org.xiu68.exp.exp10; import java.util.ArrayDeque; import java.util.ArrayList; import java.ut ...
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
随机推荐
- CMDB介绍
CMDB https://lupython.gitee.io/2018/05/05/CMDB%E4%BB%8B%E7%BB%8D/ 尚泽凯博客地址 传统运维与自动化运维的区别 传统运维: 1.项目 ...
- AIM Tech Round 5C. Rectangles 思维
C. Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- BAT批处理
常用命令 查看目录内容命令dir 指定可执行文件搜索目录path 创建目录命令md 打开指定目录命令cd 删除当前指定的子目录命令rd 改变当前盘符命令d: 文件复制命令copy 显示文本文件内容命令 ...
- 最小费用最大流模板 洛谷P3381
题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表 ...
- c/c++ 数组传参
在c/c++中,在进行数组传参时,数组的元素个数默认是不作为实参传入调用函数,也就是说c/c++ 不允许向函数传递一个完整的数组作为参数 实例: 1.形式参数是一个指针,实参包括数组长度: 1 voi ...
- VI的配置
vi下设置tab键为4个空格 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或".exrc",没有的可以新建一个.用户可以编辑它,使这些设置在 ...
- 指纹识别人脸识别 iOS
//1.判断iOS8及以后的版本 if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){ //从iPhone5S开始,出现指 ...
- Python-学习-小例子练习
网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...
- 再见NullPointerException。在Kotlin里null的处理(KAD 19)
作者:Antonio Leiva 时间:Apr 4, 2017 原文链接:https://antonioleiva.com/nullity-kotlin/ 关于Kotlin最重要的部分之一:无效处理, ...
- FlaskWeb开发从入门到放弃(二)
第5章 章节五 01 内容概要 02 内容回顾 03 面向对象相关补充:metaclass(一) 04 面向对象相关补充:metaclass(二) 05 WTforms实例化流程分析(一) 06 WT ...