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

  1. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

  2. HDU 3452 Bonsai(网络流之最小割)

    题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...

  3. 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 ...

  4. HDU 3526 Computer Assembling(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件 ...

  5. HDU 3691 Nubulsa Expo(全局最小割)

    Problem DescriptionYou may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa i ...

  6. HDU 6126.Give out candies 最小割

    Give out candies Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  7. HDU 6214 Smallest Minimum Cut 最小割,权值编码

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...

  8. UVA10480:Sabotage(最小割+输出)

    Sabotage 题目链接:https://vjudge.net/problem/UVA-10480 Description: The regime of a small but wealthy di ...

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

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

随机推荐

  1. Office2019都有哪些强大功能

    前阵子是微软一年一度的Ignite大会.而其中最引人注目.也是与我们一般人最息息相关的消息,当然是Office 2019的正式发布. 尽管Office 2019所更新的功能,对于Office 365的 ...

  2. 26个Jquery使用小技巧(转)

    下面列出了一些Jquery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字.在新窗口中打开链接.检测浏览器.预加载图片.页面样式切换.所有列等 高.动态控制页面字体大小.获得鼠标指针的X值Y值.验证元 ...

  3. li直接1px 像素的原因

    1.由于空白节点(多由于Enter造成),li不换行就可以解决问题. Internet Explorer会忽略节点之间生成的空白节点,其它浏览器不会忽略(可以通过检测节点类型,过滤子节点) 2.完美解 ...

  4. Pronunciation – The Definitive Guide to the Top 100 Words in American English

    Pronunciation – The Definitive Guide to the Top 100 Words in American English Share Tweet Share Tagg ...

  5. 使用xshell xftp连接centos的过程。

    1 配置网络 登录Centtos系统,配置网卡. cd 空格/etc/sysconfig/network-scripts  回车键. ls 查看文件 vi ifcig-eth0  编辑这个文件 修改为 ...

  6. KVM虚拟化技术(五)虚拟机管理

    一.为了提高内存.硬盘.网络的性能,需要支持半虚拟化:virtio半虚拟化驱动 二.对虚拟机的管理都是通过libvirt:所有必须要启用一个守护程序libvirtd. 三.virt-manager ① ...

  7. match()方法解析

    match()方法支持正则表达式的String对象的方法. 上篇我说了search()方法,也支持正则表达式的String对象,那么match()方法跟search()方法有什么不同呢?我们来看看. ...

  8. 让Source Insight完美支持中文注释

    如何让source insight支持中文注释,解决回车删除,移动光标出现乱码的问题?下面是解决方案:     -------Source Insight3 中文操作(左右键.删除和后退键)支持宏-- ...

  9. 吴裕雄 06-MySQL选择数据库

    实例以下实例选取了数据库 RUNOOB:use RUNOOB;注意:所有的数据库名,表名,表字段都是区分大小写的.所以你在使用SQL命令时需要输入正确的名称. 使用PHP脚本选择MySQL数据库PHP ...

  10. anchor_generator.proto:11:3: Expected "required", "optio nal", or "repeated"

    转自:https://github.com/tensorflow/models/issues/1834 When I use the commond " protoc object_dete ...