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(最小割+输出)的更多相关文章

  1. UVA10480 Sabotage —— 最小割最大流

    题目链接:https://vjudge.net/problem/UVA-10480 题解: 实际就是求最小割集. 1.什么是网络流图的“割”?答:一个边的集合,使得网络流图删除这些边之后,点被分成两部 ...

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

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

  3. uva10480(最小割)

    传送门:Sabotage 题意:给定多个城市的网络,每个城市之间的通信有花费,要求使得首都和最大城市之间的通信断掉的最小花费.要求输出任意一组砸掉的边. 分析:跑一遍最大流dinic后,根据最小割定理 ...

  4. HDU 3251 Being a Hero(最小割+输出割边)

    Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...

  5. 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)

    https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...

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

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

  7. POJ 2125 Destroying The Graph (二分图最小点权覆盖集+输出最小割方案)

    题意 有一个图, 两种操作,一种是删除某点的所有出边,一种是删除某点的所有入边,各个点的不同操作分别有一个花费,现在我们想把这个图的边都删除掉,需要的最小花费是多少. 思路 很明显的二分图最小点权覆盖 ...

  8. Expm 10_2 实现Ford-Fulkerson算法,求出给定图中从源点s到汇点t的最大流,并输出最小割。

    package org.xiu68.exp.exp10; import java.util.ArrayDeque; import java.util.ArrayList; import java.ut ...

  9. POJ 1815 Friendship(最小割+字典序输出割点)

    http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...

随机推荐

  1. mac制作U盘启动器

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.所需工具及必要条件: 1. 首先需要一个大于16GB U盘. 2.电脑系统版本应该大于10.11.X(因为之前 ...

  2. hive报错:Caused by: ERROR XBM0H: Directory /var/lib/hive/metastore/metastore_db cannot be created.

    在cdh集群中,删除之前的hive服务,然后将hive添加到其他节点,然后再通过hive客户端连接hive报错: Caused by: ERROR XJ041: Failed to create da ...

  3. android去掉button默认的点击阴影

    查了资料,发现别人都是说加一个style属性. style="?android:attr/borderlessButtonStyle" 加上了确实管用,但是我绝不是不求甚解的人.追 ...

  4. React开发时候注意点

    JSX 使用jsx的使用,用一个{}包裹起来,例如 const method = {<div> 123 </div>} 使用()小括号,防止分号自动插入 const eleme ...

  5. React切换显示和隐藏

    1 {radioChange >= 0 && 2 <div> 3 {radioChange === 0 ? ( 4 <div className={style. ...

  6. POI HSS 合并重复的列

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  7. windows本地连接腾讯云的mysql服务器

    由于最近数据库需要用上Navicat作为数据库,但是我的mysql装在腾讯云的Ubuntu上,因此需要做些配置开放端口,和监听端口,因此略显麻烦,这里记录一下连接的具体步骤,方便以后又得装(flag) ...

  8. (转)简述47种Shader Map的渲染原理与制作方法

    在Shader中会使用各种不同图参与渲染,所以简单地总结下各种图的渲染原理.制作方法,最后面几种是程序生成图. 1. Albedo 2. Diffuse(Photographic) 从上图可以看出来, ...

  9. spring boot 打包问题

    一.jar包 1.maven build package 2.linux 下执行 java -jar & 命令后台运行,也可加入服务运行 二.war包 1.将pom中的<packagin ...

  10. Xshell6连接虚拟机(一)

    普通用户转换成管理员: 一.首先进入终端: 1.输入:    su   然后回车 2.若输入密码 则显示认证失败,说明还是普通管理员身份. (1)设置新密码:        sudo passwd r ...