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对应的点连 ...
随机推荐
- Firebird日期时间操作
最近在使用Firebird数据做 一项目,使用FireBird边用边学.(以下转贴) 查询2007年度以后的,12月份以上的数据记录,datetime为timestamp字段 select * fro ...
- java script删除数组的方法集合(转载)
一.清空数组 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即被清空了 二.删 ...
- JavaScript中的setInterval用法
setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象.可以使用本动作更新来自数据库的变量或更新时间显示.setInterval动作的语法格式如下:setInterval ...
- JAVAWEB 一一框架整合(SSI : Spring+SpringMVC+ ibtis)
web.xml applicationContext.xml springmvc-servlet.xml UserController package com.ssi.controller; impo ...
- 3D模板阴影原理
3D模板阴影原理 1:先从3dsMax中导出一个简单的场景,一个园环,球,平面. 2:园环直接面向光源,园环对球体来说是一个光线的阻挡物,园环在它上面形成阴影,同时,园环和球体对平面来说是光线的阻挡物 ...
- EF AutoMaper
Mapper.CreateMap<Source,Dest>(); 该方法已弃用,使用下面这个 Mapper.Initialize(x=>x.CreateMap<Source,D ...
- Ubuntu Spark 环境搭建(转)
在安装Spark之前,我们需要在自己的系统当中先安装上jdk和scala 可以去相应的官网上下载: JDK:http://www.oracle.com/technetwork/java/javase/ ...
- Centos 7升级内核
检查当前 CentOS 系统内核版本 uname -sr 在 CentOS 7 上启用 ELRepo 仓库 rpm --import https://www.elrepo.org/RPM-GPG-KE ...
- Linux查看进程运行的完整路径方法
通过ps及top命令查看进程信息时,只能查到相对路径,查不到的进程的详细信息,如绝对路径等.这时,我们需要通过以下的方法来查看进程的详细信息: Linux在启动一个进程时,系统会在/proc下创建一个 ...
- vue-cli结构介绍
vue-cli是vue项目开发的脚手架,非常方便,其结构大致如下, 其中static是存放静态资源的,存放的静态数据可以访问到,如果在static文件夹中创建mock文件夹,在mock文件夹中创建in ...