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的电话号 ...
随机推荐
- 初步学习pg_control文件之五
接前文 初步学习pg_control文件之四,继续看何时出现 DB_IN_CRASH_RECOVERY: 看下面代码就比较清楚了:如果对 InArchiveRecovery 判断值为假,而且 读取出 ...
- easyui 验证动态添加和删除问题
$.extend($.fn.validatebox.methods, { remove: function(jq, newposition){ return jq.each(function(){ $ ...
- JAVA虚拟机(一):内存区域
根据<java虚拟机规范第二版>规定,现阶段的java内存区域总体如下图 其中,方法区和堆是所有线程共享区域. 虚拟机栈,本地方法栈,程序计数器是各线程独占. 概述一下各个区域 先说说线程 ...
- Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建
相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.4.4-nts-Win32-VC9-x86.zip 如果是win2 ...
- 孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1
孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1 (完整学习过程屏幕记录视频地址在文末) 感觉用requests获取到网页的html源代码后,更重要的工作其实是分析得到的内 ...
- dell raid配置
常用查看命令:待有dell裸机环境会详细列出 megacli -LDInfo -Lall -aALL 查raid级别 megacli -AdpAllInfo -aALL 查raid卡信息 megacl ...
- 九度OJ--Q1164
import java.util.Scanner; /* * 题目描述: * 任意输入两个9阶以下矩阵,要求判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0.90.180.270),如果不是 ...
- MySQL训练营01
一.数据库基础知识: 1. 数据库(database):保存有组织的数据的容器(通常是一个或者一组文件) 2. 数据库管理系统(DBMS):数据库软件,外界通过DBMS来创建和操纵数据库,具体是什么, ...
- Module安装
利用pip3 install numpy,代表安装了数学模块 pip3 install -U numpy,代表升级数学模块 有的时候需要升级pip本身,如上所示,直接执行pip3 install -U ...
- Vue折腾记 - (2)写一个不大靠谱的面包屑组件
先看效果图 我把页面标题和面包屑封装到一起..就不用涉及到组件的通讯了,不然又要去监听路由或者依赖状态去获取 这里写图片描述 疑惑解答: 点击父(也就是折叠菜单)为什么会跑到子菜单第一个 因为我第一个 ...