HDU3251 最大流(最小割)
Being a Hero |
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 30 Accepted Submission(s): 11 |
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 |
Sample Output
Case 1: 3 |
Source
2009 Asia Regional Ningbo Online
|
题意:
有n个点,给出m条有向边以及边的权值,f个可以选择的点以及点的权值,需要割掉一些边(耗费边权值)得到一些点(得到点权值)使得得到的价值最大,问得到的最大价值以及割掉哪些边。
代码:
//可以取得f个点连向汇点,边权值为点权值,1为源点,把给出的边连接,建图,用总的点的权值减去
//最小割就是答案(如果有连向汇点的点,减法之后就相当于没有这条边没有这个点)。
//增广路算法结束时令已标号的节点(dc.vis[u]>0的节点)集合为S,其他节点集合为T=V-S。端点分别在
//两个集合中的边就是割边。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=,inf=0x7fffffff;
int val[maxn],eu[],ev[],ew[];
struct edge{
int from,to,cap,flow;
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct dinic{
int n,m,s,t;
vector<edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n){
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap){
edges.push_back(edge(from,to,cap,));
edges.push_back(edge(to,from,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)g[x].size();i++){
edge&e=edges[g[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a){
if(x==t||a==) return a;
int flow=,f;
for(int&i=cur[x];i<(int)g[x].size();i++){
edge&e=edges[g[x][i]];
if(d[x]+==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[g[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;this->t=t;
int flow=;
while(bfs()){
memset(cur,,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
}dc;
int main()
{
int t,n,m,f,a,c,cas=;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&f);
dc.init(n+);
int s=,t=n+;
for(int i=;i<=m;i++){
scanf("%d%d%d",&eu[i],&ev[i],&ew[i]);
dc.addedge(eu[i],ev[i],ew[i]);
}
memset(val,,sizeof(val));
int sum=;
for(int i=;i<=f;i++){
scanf("%d%d",&a,&c);
val[a]=c;
sum+=c;
dc.addedge(a,t,c);
}
int ans=dc.maxflow(s,t);
printf("Case %d: %d\n",++cas,sum-ans);
int cnt=,nu[];
for(int i=;i<=m;i++){
if(dc.vis[eu[i]]>&&dc.vis[ev[i]]==&&ev[i]!=n+)
nu[++cnt]=i;
}
printf("%d",cnt);
for(int i=;i<=cnt;i++) printf(" %d",nu[i]);
printf("\n");
}
return ;
}
HDU3251 最大流(最小割)的更多相关文章
- 最大流-最小割 MAXFLOW-MINCUT ISAP
简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...
- 最大流&最小割 - 专题练习
[例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...
- UVa11248 Frequency Hopping(最大流+最小割)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206 [思路] 最大流最小割. 可以确定的是如果不可行需要修改的 ...
- matlab练习程序(最大流/最小割)
学习这个算法是为学习图像处理中的图割算法做准备的. 基本概念: 1.最大流是一个有向图. 2.一个流是最大流,当且仅当它的残余网络中不包括增广路径. 3.最小割就是网络中所有割中值最小的那个割,最小割 ...
- 「网络流24题」「LuoguP2774」方格取数问题(最大流 最小割
Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.对于给定的方 ...
- HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...
- ISAP 最大流 最小割 模板
虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...
- Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并
A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...
- 网络流 最大流—最小割 之SAP算法 详解
首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...
- nyoj-677-最大流最小割
677-碟战 内存限制:64MB 时间限制:2000ms 特判: No通过数:2 提交数:2 难度:4 题目描述: 知己知彼,百战不殆!在战争中如果被敌人掌握了自己的机密,失败是必然的.K国在一场战争 ...
随机推荐
- BOM / URL编码解码 / 浏览器存储
BOM 浏览器对象模型 BOM(Browser Object Model) 是指浏览器对象模型,是用于描述这种对象与对象之间层次关系的模型,浏览器对象模型提供了独立于内容的.可以与浏览器窗口进行互动的 ...
- markdown语法介绍
1. 标题类 每级标题用"# title"表示,共支持6级标题: 2. 段落类 1.建议用换行符控制: 2.用"<p></p>"控制: ...
- Stunnel客户端安装和配置
声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(DCS)的用户指南. 本文以公网访问华为云分布式缓存服务的Redis缓存为示例,介绍Stunnel客户端在各操作系统下的安装和配置. Stunn ...
- 珍珠 Median Weight Bead 977
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
- matconv-GPU 编译问题
如出现以下错误: 1 error detected in the compilation of "C:/Users/Justin/AppData/Local/Temp/tmpxft_0000 ...
- DataTable转Json,Json转DataTable
// 页面加载时 /// </summary> /// <param name="sender"></param> /// <param ...
- java通过控制鼠标实现屏幕广播
在java实现屏幕共享的小程序中提到截取屏幕时是没鼠标,为了看到教师端界面上的鼠标,可以在截取屏幕的时候,把鼠标绘制到每一张截图上去,但是由于截图的时候是一张张截取的,所以看到的鼠标难免会有点卡,之前 ...
- js中斜杠转义
js中“/”不需要转义. if(myPath.indexOf("/Upload/EmailFile/")!=-1){ alert("有附件!")}
- linux的几个发行网站
Red Hat: http://www.redhat.com Fedora: http://fedoraproject.org/ Mandriva: http://www.mandriva ...
- Delphi 制作自定义数据感知控件并装入包(dpk文件)中(与DBText类似的数据感知控件)
一.基础知识简介: 1.包的命名介绍: 包的命名没有规则,但是建议遵守包的命名约定:包的命名与包的版本相关,包的名称前面几个字符通常表示作者或公司名,也可以是控件的一个描述词,后面紧跟的Std表示运行 ...