TZOJ 5110 Pollutant Control(边数最少最小割最小字典序输出)
描述
It's your first day in Quality Control at Merry Milk Makers, and already there's been a catastrophe: a shipment of bad milk has been sent out. Unfortunately, you didn't discover this until the milk was already into your delivery system on its way to stores. You know which grocer that milk was destined for, but there may be multiple ways for the milk to get to that store.
The delivery system is made up of a several warehouses, with trucks running from warehouse to warehouse moving milk. While the milk will be found quickly, it is important that it does not make it to the grocer, so you must shut down enough trucks to ensure that it is impossible for the milk to get to the grocer in question. Every route costs a certain amount to shut down. Find the minimum amount that must be spent to ensure the milk does not reach its destination, along with a set of trucks to shut down that achieves this goal at that cost.
输入
Line 1: Two space separated integers, N and M. N (2 <= N <= 32) is the number of warehouses that Merry Milk Makers has, and M (0 <= M <= 1000) is the number of trucks routes run. Warehouse 1 is actually the productional facility, while warehouse N is the grocer to which which the bad milk was destined.
Line 2..M+1: Truck routes: three space-separated integers, Si, Ei, and Ci. Si and Ei (1 <= Si,Ei <= N) correspond to the pickup warehouse and dropoff warehouse for the truck route. Ci (0 <= Ci <= 2,000,000) is the cost of shutting down the truck route.
输出
The first line of the output should be two integers, C and T. C is the minimum amount which must be spent in order to ensure the our milk never reaches its destination. T is the minimum number of truck routes that you plan to shut down in order to achive this goal. The next T lines sould contain a sorted list of the indexes of the truck routes that you suggest shutting down. If there are multiple sets of truck routes that achieve the goal at minimum cost, choose one that shuts down the minimum number of routes. If there are still multiple sets, choose the one whose initial routes have the smallest index.
样例输入
4 5
1 3 100
3 2 50
2 4 60
1 2 40
2 3 80
样例输出
60 1
3
题意
N个点,M条有向边,求最小割,并且输出割了几条边,并且输出最小字典序。
题解
问题1就是个最小割。
问题2边数,相当于把边权哈希成w=w*1001+1,这样最小割%1001就是边数,而且可以保证边数最少。
问题3最小字典序,假设一条边是最小割集,那么删掉这条边w=w*1001+0后,最小割变小,若未变小则恢复这条边。
为什么删边不把w=0呢?(具体样例见代码最后)把w=0后,相当于把路断开了,最小割也变小了,但是不是我们需要的边。
根据最小割,边一定是路上的最小值,如果这条边是路上的最小值,那么你把w=w*1001+0后,最小割变小了。
如果这条边不是路上的最小值,那么你把w=w*1001+0后,最小割不变。
WA了两次一次w=0,一次忘记恢复了(忘记恢复就相当于自动把w=0了)。
代码
#include<bits/stdc++.h>
using namespace std; #define LL long long const int maxn=1e5+;
const int maxm=2e5+;
const int INF=0x3f3f3f3f; int TO[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[];
LL CAP[maxm],W[];
int n,m,S,T;
int U[],V[];
bool VIS[]; void add(int u,int v,LL 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];
}
}
LL dfs(int u,LL fl)
{
if(u==T)return fl;
LL flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
LL 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;
}
LL ISAP()
{
bfs();
LL ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int main()
{
init();
int N,M;
scanf("%d%d",&N,&M);
for(int i=;i<=M;i++)
{
scanf("%d%d%lld",&U[i],&V[i],&W[i]);
add(U[i],V[i],W[i]*+);
VIS[i]=;
}
S=,T=N,n=T;
LL ans=ISAP();
printf("%lld %lld\n",ans/,ans%);
for(int i=;i<=M;i++)
{
init();
VIS[i]=;
for(int j=;j<=M;j++)
add(U[j],V[j],W[j]*+VIS[j]);
LL bns=ISAP();
if(bns<ans)
{
ans=bns;
printf("%d\n",i);
}
else
VIS[i]=;
//printf("i=%d %lld %lld\n",i,bns/1001,bns%1001);
}
return ;
}
/*
5 5
2 4 30
1 2 30
4 5 40
1 3 50
3 5 30
*/
TZOJ 5110 Pollutant Control(边数最少最小割最小字典序输出)的更多相关文章
- 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流
最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...
- 【BZOJ-4519】不同的最小割 最小割树(分治+最小割)
4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 393 Solved: 239[Submit][Stat ...
- 【BZOJ-2229】最小割 最小割树(最大流+分治)
2229: [Zjoi2011]最小割 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1565 Solved: 560[Submit][Status ...
- BZOJ4519[Cqoi2016]不同的最小割——最小割树+map
题目描述 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成 两个部分,如果结点s,t不在同一个部分中,则称这个划分是关于s,t的割.对于带权图来说,将 所有顶点处在 ...
- BZOJ2229[Zjoi2011]最小割——最小割树
题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- BZOJ1797:[AHOI2009]最小割(最小割)
Description A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路.设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站 ...
- 【BZOJ4519】[Cqoi2016]不同的最小割 最小割树
[BZOJ4519][Cqoi2016]不同的最小割 Description 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分 ...
- 【BZOJ2229】[Zjoi2011]最小割 最小割树
[BZOJ2229][Zjoi2011]最小割 Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有 ...
随机推荐
- 如何为ABAP程序添加权限检查
一.确认权限对象,及其关联字段: TCode: SU21 例如权限对象"M_MSEG_WMB",它关联字段为"WERKS",详见下图: 二.在ABAP代码中添加 ...
- iOS逆向系列-theos
概述 theos是GitHub开源的一个项目,通过nic.pl创建tweak项目.通过编写我们注入代码,然后执行编译.打包.安装等操作将代码注入iPhone安装的制定程序. theos环境配置 安装签 ...
- 连接 MySQL 报错:Lost connection to MySQL server at 'reading authorization packet', system error: 34
报错信息: Lost connection to MySQL server at 解决方案: use mysql; ; flush privileges; 参考: https://blog.csdn. ...
- UMP系统架构
- Linux-c 线程锁
typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...
- [JZOJ3296] 【SDOI2013】刺客信条
题目 题目大意 给你一棵树,树上每个节点有000或111的状态. 用最少的操作次数使得当前状态与目标状态同构. 思考历程 首先想到的是找重心. 因为根是不确定的,但重心只会有一个或两个,以重心为根就能 ...
- 廖雪峰Java12maven基础-1maven入门-1maven介绍
1.Java项目: * 1.1.首先确定引入哪些依赖包,如commons-logging, log4j, selenium * 1.2.确定项目的目录结构 * 1.3.配置环境:如JDK的版本号 * ...
- 一个windows 两个jar
设置两个子JAVA_HOME,一个总设置两个子JAVA_HOME:JAVA_HOME6 = C:\Program Files\Java\jdk1.6.0_43JAVA_HOME8 = C:\Progr ...
- pyQT Dialog默认选中某一个选项问题的解决
方法一: 在新建ui文件时不要新建Dialog # -*- coding: utf-8 -*- # Form implementation generated from reading ui file ...
- SQLServer索引的四个高级特性
一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引 SQLSer ...