博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2435 There is a war (网络流-最小割)
阅读量:5235 次
发布时间:2019-06-14

本文共 4593 字,大约阅读时间需要 15 分钟。

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
 
4 4 0 4 2 1 2 2 3 4 2 4 3 1 2 1 2 3 1 3 4 10 4 3 1 2 5 2 3 2 3 4 3
 

Sample Output
 
0 2 1 3
 

Source
 

题目大意:

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

解题思路:

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

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

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

解题代码:

#include 
#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;const int maxn=100,maxm=10000;struct edge{ int u,v,f,next;}e[maxm+10];int src,sink,cnt,head[maxn+10];int visited[maxn+10],marked;int dist[maxn+10];int n,m;void adde(int u,int v,int f){ e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++; e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;}void bfs(){ marked++; for(int i=0;i<=sink;i++) dist[i]=0; queue
q; visited[src]=marked; q.push(src); while(!q.empty()){ int s=q.front(); q.pop(); for(int i=head[s];i!=-1;i=e[i].next){ int d=e[i].v; if(e[i].f>0 && visited[d]!=marked ){ q.push(d); dist[d]=dist[s]+1; visited[d]=marked; } } }}int dfs(int u,int delta){ if(u==sink) return delta; else{ int ret=0; for(int i=head[u];delta && i!=-1;i=e[i].next){ if(e[i].f>0 && dist[e[i].v]==dist[u]+1){ int d=dfs(e[i].v,min(e[i].f,delta)); e[i].f-=d; e[i^1].f+=d; delta-=d; ret+=d; } } return ret; }}int maxflow(){ int ret=0; while(true){ bfs(); if(visited[sink]!=marked) return ret; ret+=dfs(src,INF); } return ret;}void initial(){ cnt=0; memset(head,-1,sizeof(head)); marked++;}void input(){ scanf("%d%d",&n,&m); src=1,sink=n; while(m-- >0){ int u,v,w; scanf("%d%d%d",&u,&v,&w); adde(u,v,w); }}void solve(){ int ans=maxflow(); bool f[maxn+10]; memset(f,false,sizeof(f)); queue
q; q.push(src); f[src]=true; while(!q.empty() ){ int s=q.front(); q.pop(); for(int i=head[s];i!=-1;i=e[i].next){ int d=e[i].v; if(e[i].f>0 && !f[d]){ q.push(d); f[d]=true; } } } vector
v; for(int i=0;i
tmp) tmp=flow; cnt-=2; head[i]=headu,head[j]=headv; for(int t=0;t
0){ initial(); input(); solve(); } return 0;}

转载于:https://www.cnblogs.com/toyking/p/3893173.html

你可能感兴趣的文章
Objective-C中的@property和@synthesize用法
查看>>
jsp连接数据库
查看>>
一位面试者提到直接调用vuex中mutations方法
查看>>
安装JDK
查看>>
semantic ui要装什么才能使用
查看>>
四叶草社交平台——十天冲刺(10)
查看>>
Linux 2.6 完全公平调度算法CFS(Completely Fair Scheduler)分析
查看>>
海量数据处理面试题集锦
查看>>
【设计模式】命令模式
查看>>
pyinstaller---将py文件打包成exe
查看>>
readonly和const的区别
查看>>
SSM框架搭建(四) springmvc和mybatis的配置
查看>>
UVa 11346 - Probability
查看>>
python数据类型之间的转换
查看>>
微软职位内部推荐-SDEII
查看>>
微软职位内部推荐-SENIOR SOFTWARE ENGINEER
查看>>
Redis系统性介绍
查看>>
(备忘)打开office2010总是在配置进度
查看>>
jquery中的ajax方法(备忘)
查看>>
iOS基础-高级视图-UITableView--静态单元格
查看>>