HDU 3251 Being a Hero(最小割+输出割边)
Problem Description
You are the hero who saved your country. As promised, the king will give you some cities of the country, and you can choose which ones to own!
But don't get too excited. The cities you take should NOT be reachable from the capital -- the king does not want to accidentally enter your area. In order to satisfy this condition, you have to destroy some roads. What's worse, you have to pay for that -- each road is associated with some positive cost. That is, your final income is the total value of the cities you take, minus the total cost of destroyed roads.
Note that each road is a unidirectional, i.e only one direction is available. Some cities are reserved for the king, so you cannot take any of them even if they're unreachable from the capital. The capital city is always the city number 1.
Input
The first line contains a single integer T (T <= 20), the number of test cases. Each case begins with three integers n, m, f (1 <= f < n <= 1000, 1 <= m < 100000), the number of cities, number of roads, and number of cities that you can take. Cities are numbered 1 to n. Each of the following m lines contains three integers u, v, w, denoting a road from city u to city v, with cost w. Each of the following f lines contains two integers u and w, denoting an available city u, with value w.
Output
For each test case, print the case number and the best final income in the first line. In the second line, print e, the number of roads you should destroy, followed by e integers, the IDs of the destroyed roads. Roads are numbered 1 to m in the same order they appear in the input. If there are more than one solution, any one will do.
Sample Input
2
4 4 2
1 2 2
1 3 3
3 2 4
2 4 1
2 3
4 4
4 4 2
1 2 2
1 3 3
3 2 1
2 4 1
2 3
4 4
Sample Output
Case 1: 3
1 4
Case 2: 4
2 1 3
题意
N个城市M条边F个城市,M行每行u,v,w代表破坏u到v需要花费w元,F行u,w代表1不能到城市u获得w元,问你最大利润然后输出破坏的道路编号
题解
建立源点S=1,汇点T=n+1,uv连边流量w,uT连边流量w,跑最小割,得到最小花费,用总钱-最小花费就是利润
输出路径考虑S和T集合,DFS(1)跑出来的是S集合的点,其余都是T集合的点,枚举m条边,如果u输出S集合,v属于T集合,就说明是割边
代码
#include<bits/stdc++.h>
using namespace std; const int maxn=;
const int maxm=2e5+;//至少总M*2
const int INF=0x3f3f3f3f; int TO[maxm],CAP[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[],vis[maxn],u[maxm],v[maxm];
int n,m,S,T; void add(int u,int v,int cap)
{
//printf("i=%d %d %d %d\n",tote,u,v,cap);
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
void bfs()
{
memset(gap,,sizeof gap);
memset(d,,sizeof d);
++gap[d[T]=];
for(int i=;i<=n;++i)cur[i]=FIR[i];
int head=,tail=;
q[]=T;
while(head<=tail)
{
int u=q[head++];
for(int v=FIR[u];v!=-;v=NEXT[v])
if(!d[TO[v]])
++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
}
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
if(!fl)return flow;
}
if(!(--gap[d[u]]))d[S]=n+;
++gap[++d[u]],cur[u]=FIR[u];
return flow;
}
int ISAP()
{
bfs();
int ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
void dfs(int u)
{
for(int i=FIR[u];i!=-;i=NEXT[i])
{
int v=TO[i];
if(CAP[i]!=&&!vis[v])
{
vis[v]=true;
dfs(v);
}
}
}
int main()
{
int t,f,o=;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d%d",&n,&m,&f);
for(int i=,w;i<m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w);
add(u[i],v[i],w);
}
S=,T=n+,n+=;
add(S,,INF);
int sum=;
for(int i=,uu,w;i<f;i++)
{
scanf("%d%d",&uu,&w);
add(uu,T,w);
sum+=w;
}
printf("Case %d: %d\n",o++,sum-ISAP()); memset(vis,,sizeof vis);
dfs();
vector<int>ans;
for(int i=;i<m;i++)
if(vis[u[i]]&&!vis[v[i]])
ans.push_back(i+);
printf("%d",(int)ans.size());
for(int i=;i<ans.size();i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}
HDU 3251 Being a Hero(最小割+输出割边)的更多相关文章
- HDU 4289:Control(最小割)
		http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ... 
- HDU 3452 Bonsai(网络流之最小割)
		题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ... 
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
		Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ... 
- HDU 3526 Computer Assembling(最小割)
		http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件 ... 
- HDU 3691 Nubulsa Expo(全局最小割)
		Problem DescriptionYou may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa i ... 
- HDU 6126.Give out candies 最小割
		Give out candies Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ... 
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ... 
- UVA10480:Sabotage(最小割+输出)
		Sabotage 题目链接:https://vjudge.net/problem/UVA-10480 Description: The regime of a small but wealthy di ... 
- 洛谷 P4174 [NOI2006]最大获利 &&  洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)
		https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ... 
随机推荐
- Linux MySQL 安装、远程访问和密码重置
			安装: yum install mysql yum install mysql-server yum install mysql-devel 设置开机启动: chkconfig -add mysqld ... 
- [AS3]as3中splice和slice的用法介绍说明
			splice 删除数组一段连续的元素,返回被删除的元素数组 var arr:Array = ["a","b","c","d&quo ... 
- APP-8-文本语音
			1.百度语音合成JS文件 baidu_tts_cors.js /** * 浏览器调用语音合成接口 * @param {Object} param 百度语音合成接口参数 * 请参考 https://ai ... 
- How to Pronounce the Numbers 1 – 10
			How to Pronounce the Numbers 1 – 10 Share Tweet Share Tagged With: Numbers Numbers are something you ... 
- Linux性能测试分析命令_top
			top命令动态展示系统整体资源和各个进程资源占用状况,是Linux下常用的性能分析工具. top命令语法 使用格式:top [-] [d] [b] [H] [p] [q] [c] [C] [S] [s ... 
- Hydra密码破译工具
			Hydra简介 Hydra是著名黑客组织thc开发的一款开源的暴力密码破解工具,可以在线破解多种密码,目前已经被Backtrack和kali等渗透平台收录.除了命令行下的Hydra外,还提供了Hydr ... 
- 使用支持向量机(SVM) 算法进行分类
			1 支持向量机(SVM)的基本概念 SVM是一种分类算法,其侧重于模式识别方面.使用SVM可以大大提高分类的准确性. 分类相当于模式识别的子集合,模式识别重点在于对已知数据进行特征发现与提取. ... 
- Linux下MySQL5.7.18二进制包安装(手动添加配置文件my_default.cnf)
			本文出处:http://www.cnblogs.com/wy123/p/6815049.html 最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5. ... 
- 法门扫地僧总结vue面试题(部分来源网络)
			Front-End 前端开发工程师面试宝典! (本文部分有转载,不定期更新!) 前言(README.md) 本仓库是我整理的前端常见面试题,大部分由我整理,其中个别部分参考 ... 
- 浏览器唤起APP的思路(本文转载)
			在做 h5 页面中,会遇到这样一个需求,有一个立即打开的按钮,如果本地安装了我们的 app,那么点击就直接唤起本地 app,如果没有安装,则跳转到下载. 首先想到的是两个问题:一是如何唤起本地 app ... 
