There is a war

Problem Description
      There is a sea.

      There are N islands in the sea.

      There are some directional bridges connecting these islands.

      There is a country called Country One located in Island 1.

      There is another country called Country Another located in Island N. 

      There is a war against Country Another, which launched by Country One.

      There is a strategy which can help Country Another to defend this war by destroying the bridges for the purpose of making Island 1 and Island n disconnected.

      There are some different destroying costs of the bridges.

      There is a prophet in Country Another who is clever enough to find the minimum total destroying costs to achieve the strategy.

      There is an architecture in Country One who is capable enough to rebuild a bridge to make it unbeatable or build a new invincible directional bridge between any two countries from the subset of island 2 to island n-1.

      There is not enough time for Country One, so it can only build one new bridge, or rebuild one existing bridge before the Country Another starts destroying, or do nothing if happy.

      There is a problem: Country One wants to maximize the minimum total destroying costs Country Another needed to achieve the strategy by making the best choice. Then what’s the maximum possible result?
 
Input
      There are multiple cases in this problem.

      There is a line with an integer telling you the number of cases at the beginning.

      The are two numbers in the first line of every case, N(4<=N<=100) and M(0<=M<=n*(n-1)/2), indicating the number of islands and the number of bridges.

      There are M lines following, each one of which contains three integers a, b and c, with 1<=a, b<=N and 1<=c<=10000, meaning that there is a directional bridge from a to b with c being the destroying cost.

      There are no two lines containing the same a and b.
 
Output
      There is one line with one integer for each test case, telling the maximun possible result.
 
Sample Input
  1. 4
  2. 4 0
  3. 4 2
  4. 1 2 2
  5. 3 4 2
  6. 4 3
  7. 1 2 1
  8. 2 3 1
  9. 3 4 10
  10. 4 3
  11. 1 2 5
  12. 2 3 2
  13. 3 4 3
 
Sample Output
  1. 0
  2. 2
  3. 1
  4. 3
 
Source
 

题目大意:

n个岛通过有向边连在一起,countryOne坐落在1号岛上,countryAny坐落在n号岛上,如今1 要进攻 n ,n为了抵御1的攻击,要毁坏边,没条边毁坏要花费,如今1能够在 2-n 随意两个岛上建立一个摧毁不了的边,使得 countryAny 为了抵御进攻最小的花费最大为多少?

解题思路:

首先,最小割能够理解成网络流的流量,第一步 。countryAny肯定要用最小的花费使得图不连通,这个花费就是最小割。

可是,countryOne这个时候能够再建一条边使得图再次连通,所以得找出最小割的边集,边集就是  从 countryOne相邻的点 到 与countryOne不相邻的点 连接的边,

所以。再枚举这条边。加入到残余网络中。求全部答案中最大的与之前的最小割相加就是答案。

解题代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. const int INF=0x3f3f3f3f;
  8. const int maxn=100,maxm=10000;
  9.  
  10. struct edge{
  11. int u,v,f,next;
  12. }e[maxm+10];
  13.  
  14. int src,sink,cnt,head[maxn+10];
  15. int visited[maxn+10],marked;
  16. int dist[maxn+10];
  17. int n,m;
  18.  
  19. void adde(int u,int v,int f){
  20. e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
  21. e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
  22. }
  23.  
  24. void bfs(){
  25. marked++;
  26. for(int i=0;i<=sink;i++) dist[i]=0;
  27. queue <int> q;
  28. visited[src]=marked;
  29. q.push(src);
  30. while(!q.empty()){
  31. int s=q.front();
  32. q.pop();
  33. for(int i=head[s];i!=-1;i=e[i].next){
  34. int d=e[i].v;
  35. if(e[i].f>0 && visited[d]!=marked ){
  36. q.push(d);
  37. dist[d]=dist[s]+1;
  38. visited[d]=marked;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. int dfs(int u,int delta){
  45. if(u==sink) return delta;
  46. else{
  47. int ret=0;
  48. for(int i=head[u];delta && i!=-1;i=e[i].next){
  49. if(e[i].f>0 && dist[e[i].v]==dist[u]+1){
  50. int d=dfs(e[i].v,min(e[i].f,delta));
  51. e[i].f-=d;
  52. e[i^1].f+=d;
  53. delta-=d;
  54. ret+=d;
  55. }
  56. }
  57. return ret;
  58. }
  59. }
  60.  
  61. int maxflow(){
  62. int ret=0;
  63. while(true){
  64. bfs();
  65. if(visited[sink]!=marked) return ret;
  66. ret+=dfs(src,INF);
  67. }
  68. return ret;
  69. }
  70.  
  71. void initial(){
  72. cnt=0;
  73. memset(head,-1,sizeof(head));
  74. marked++;
  75. }
  76.  
  77. void input(){
  78. scanf("%d%d",&n,&m);
  79. src=1,sink=n;
  80. while(m-- >0){
  81. int u,v,w;
  82. scanf("%d%d%d",&u,&v,&w);
  83. adde(u,v,w);
  84. }
  85. }
  86.  
  87. void solve(){
  88. int ans=maxflow();
  89. bool f[maxn+10];
  90. memset(f,false,sizeof(f));
  91. queue <int> q;
  92. q.push(src);
  93. f[src]=true;
  94. while(!q.empty() ){
  95. int s=q.front();
  96. q.pop();
  97. for(int i=head[s];i!=-1;i=e[i].next){
  98. int d=e[i].v;
  99. if(e[i].f>0 && !f[d]){
  100. q.push(d);
  101. f[d]=true;
  102. }
  103. }
  104. }
  105.  
  106. vector <edge> v;
  107. for(int i=0;i<cnt;i++) v.push_back(e[i]);
  108.  
  109. int tmp=0;
  110. for(int i=2;i<=n-1;i++){
  111. for(int j=2;j<=n-1;j++){
  112. if( f[i] && ( ! f[j] ) ){
  113. int headu=head[i],headv=head[j];
  114.  
  115. adde(i,j,INF);
  116. int flow=maxflow();
  117. if(flow>tmp) tmp=flow;
  118.  
  119. cnt-=2;
  120. head[i]=headu,head[j]=headv;
  121. for(int t=0;t<cnt;t++) e[t]=v[t];
  122. }
  123. }
  124. }
  125. cout<<ans+tmp<<endl;
  126. }
  127.  
  128. int main(){
  129. int t;
  130. scanf("%d",&t);
  131. while(t-- >0){
  132. initial();
  133. input();
  134. solve();
  135. }
  136. return 0;
  137. }

HDU 2435 There is a war (网络流-最小割)的更多相关文章

  1. HDU 2435 There is a war Dinic 最小割

    题意是有n座城市,n号城市不想让1号城市可达n号,每条道路有一条毁坏的代价,1号还可以修一条不能毁坏的道路,求n号城市所需的最小代价最大是多少. 毁坏的最小代价就直接求一遍最大流,就是最小割了.而可以 ...

  2. HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】

    Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...

  3. 【hdu 4859】海岸线(图论--网络流最小割)

    题意:有一个区域,有'.'的陆地,'D'的深海域,'E'的浅海域.其中浅海域可以填充为陆地.这里的陆地区域不联通,并且整个地图都处在海洋之中.问填充一定浅海域之后所有岛屿的最长的海岸线之和. 解法:最 ...

  4. 【题解】 bzoj3894: 文理分科 (网络流/最小割)

    bzoj3894,懒得复制题面,戳我戳我 Solution: 首先这是一个网络流,应该还比较好想,主要就是考虑建图了. 我们来分析下题面,因为一个人要么选文科要么选理科,相当于两条流里面割掉一条(怎么 ...

  5. 【bzoj3774】最优选择 网络流最小割

    题目描述 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择了的.一个点如果被选择了,那么可以得到Bij ...

  6. 【bzoj1143】[CTSC2008]祭祀river Floyd+网络流最小割

    题目描述 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河 ...

  7. 【bzoj1797】[Ahoi2009]Mincut 最小割 网络流最小割+Tarjan

    题目描述 给定一张图,对于每一条边询问:(1)是否存在割断该边的s-t最小割 (2)是否所有s-t最小割都割断该边 输入 第一行有4个正整数,依次为N,M,s和t.第2行到第(M+1)行每行3个正 整 ...

  8. 【bzoj1976】[BeiJing2010组队]能量魔方 Cube 网络流最小割

    题目描述 一个n*n*n的立方体,每个位置为0或1.有些位置已经确定,还有一些需要待填入.问最后可以得到的 相邻且填入的数不同的点对 的数目最大. 输入 第一行包含一个数N,表示魔方的大小. 接下来 ...

  9. 【bzoj4177】Mike的农场 网络流最小割

    题目描述 Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不同,其中第i个牲畜围栏中的动物长大后,每只牛可以卖a[i] ...

  10. 【bzoj3438】小M的作物 网络流最小割

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801522.html 题目描述 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物 ...

随机推荐

  1. 母函数&&排列(模板)

    #include <iostream> #include <algorithm> using namespace std; int main() { int n,i; int ...

  2. Android学习手记(4) BroadcastReceiver监听电池信息

    Android 中,Broadcast是一种在应用程序之间进行传输信息的机制.BroadcastReceiver对发送过来的Broadcast进行过滤和响应.根据这种机制,我们可以获取电池现有电量等信 ...

  3. MySql中的时间类型datetime,timestamp,date,year比较

    MySQL日期类型.日期格式.存储空间.日期范围比较.日期类型        存储空间       日期格式                 日期范围------------ ---------   ...

  4. 学习OpenSeadragon之四(导航视图)

    OpenSeadragon介绍以及上手:学习OpenSeadragon之一 OpenSeadragon主要用于地图.医学图像等需要放大缩小分层显示的图像显示. 1.简单例子 导航视图就是在一个小框中显 ...

  5. redis数据结构HyperLogLog

    如果我们要实现记录网站每天访问的独立IP数量这样的一个功能 集合实现: 使用集合来储存每个访客的 IP ,通过集合性质(集合中的每个元素都各不相同)来得到多个独立 IP ,然后通过调用 SCARD 命 ...

  6. 读书笔记 -part1

    自从毕业以后到现在~看的书是越来越少了 の其实好像貌似从来没有认认真真的看书  除非工作遇到难于解决的问题迫不得已才去翻书看 有些问题也是莫名其妙的就这样解决了  于是乎被人美名其曰“高人”或&quo ...

  7. python3中文字符编码问题

    最近在进行网络爬虫时,被中文的编码问题搞得很头疼,特别是在windows环境下. 1.爬取到的中文网页内容,在解析时出现解析错误 一般情况下,我们都是用urllib中的相关函数,进行web页面的爬取, ...

  8. 《python基础教程》笔记之 序列通用操作

    索引 序列中的所有元素都是有编号的--从0开始递增.使用负数索引时,Python会从右边,也就是从最后一个元素开始计数,最后一个元素的位置编号是-1.此外,字符串是一个有字符组成的序列,字符串字面值可 ...

  9. 向null地址copy数据和不断改变指针指向

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  10. 信息安全实验二:return-to-libc

    title: return-to-libc date: 2016-01-11 17:40:30 categories: information-security tags: return-to-lib ...