UVA - 10480 Sabotage (Dinic)
Posted zgqblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 10480 Sabotage (Dinic)相关的知识,希望对你有一定的参考价值。
The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebel-
lion. Because of the enormous disturbances this is causing in world economy, an imperialist military
super power has decided to invade the country and reinstall the old regime.
For this operation to be successful, communication between the capital and the largest city must
be completely cut. This is a difficult task, since all cities in the country are connected by a computer
network using the Internet Protocol, which allows messages to take any path through the network.
Because of this, the network must be completely split in two parts, with the capital in one part and
the largest city in the other, and with no connections between the parts.
There are large differences in the costs of sabotaging different connections, since some are much
more easy to get to than others.
Write a program that, given a network specification and the costs of sabotaging each connection,
determines which connections to cut in order to separate the capital and the largest city to the lowest
possible cost.
Input
Input file contains several sets of input. The description of each set is given below.
The first line of each set has two integers, separated by a space: First one the number of cities, n in
the network, which is at most 50. The second one is the total number of connections, m, at most 500.
The following m lines specify the connections. Each line has three parts separated by spaces: The
first two are the cities tied together by that connection (numbers in the range 1 − n). Then follows the
cost of cutting the connection (an integer in the range 1 to 40000000). Each pair of cites can appear
at most once in this list.
Input is terminated by a case where values of n and m are zero. This case should not be processed.
For every input set the capital is city number 1, and the largest city is number 2.
Output
For each set of input you should produce several lines of output. The description of output for each set
of input is given below:
The output for each set should be the pairs of cities (i.e. numbers) between which the connection
should be cut (in any order), each pair on one line with the numbers separated by a space. If there is
more than one solution, any one of them will do.
Print a blank line after the output for each set of input.
题意:
求图的最小割的可能方案。
思路:
暴力枚举每一条边,边的全值是否是w,判断删除之后最大流是否会减少w,如果会的话,那就真的删了它,否则还原图。
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl; #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl; #define ls (t<<1) #define rs ((t<<1)|1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int maxm = 100086; const int inf = 0x3f3f3f3f; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int Head[maxn],cnt; struct edge int Next,u,v; int w; e[maxm]; void add_edge(int u,int v,int w) e[cnt].Next=Head[u]; e[cnt].v=v; e[cnt].u=u; e[cnt].w=w; Head[u]=cnt++; int n,m; int D_vis[maxn],D_num[maxn]; int source,meeting; bool bfs() memset(D_vis,0,sizeof(D_vis)); for(int i=0;i<=n;i++)//注意要覆盖所有点 D_num[i]=Head[i]; D_vis[source]=1; queue<int>q; q.push(source); int r=0; while(!q.empty()) int u=q.front(); q.pop(); int k=Head[u]; while(k!=-1) if(!D_vis[e[k].v]&&e[k].w) D_vis[e[k].v]=D_vis[u]+1; q.push(e[k].v); k=e[k].Next; return D_vis[meeting]; int dfs(int u,int f) if(u==meeting)return f; int &k=D_num[u]; while(k!=-1) if(D_vis[e[k].v]==D_vis[u]+1&&e[k].w) int d=dfs(e[k].v,min(f,e[k].w)); if(d>0) e[k].w-=d; e[k^1].w+=d; return d; k=e[k].Next; return 0; int Dinic() int ans=0; while(bfs()) int f; while((f=dfs(source,inf))>0) ans+=f; return ans; int main() // ios::sync_with_stdio(false); // freopen("in.txt", "r", stdin); while (scanf("%d%d",&n,&m)!=EOF&&n) memset(Head,-1,sizeof(Head)); cnt=0; source=1;meeting=2; for(int i=1;i<=m;i++) int x,y,z; scanf("%d%d%d",&x,&y,&z); add_edge(x,y,z); add_edge(y,x,z); int tot = Dinic(); for(int j=0;j<cnt;j+=2) e[j].w=e[j^1].w=(e[j].w+e[j^1].w)/2; for(int i=0;i<cnt;i+=2) int w = e[i].w; e[i].w=e[i^1].w=0; int tmp = Dinic(); // fuck(tmp) for(int j=0;j<cnt;j+=2) e[j].w=e[j^1].w=(e[j].w+e[j^1].w)/2; if(w==tot - tmp) printf("%d %d\n",e[i].u,e[i].v); tot=tmp; else e[i].w=e[i^1].w=w; if(tot<=0) break; printf("\n"); return 0;
看了网上的题解,发现了更好的方案。
求出最大流之后,图上的点已经被分为了两部分,连接两个部分的边就是一种可能的方案。
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl; #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl; #define ls (t<<1) #define rs ((t<<1)|1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int maxm = 100086; const int inf = 0x3f3f3f3f; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int Head[maxn],cnt; struct edge int Next,u,v; int w; e[maxm]; void add_edge(int u,int v,int w) e[cnt].Next=Head[u]; e[cnt].v=v; e[cnt].u=u; e[cnt].w=w; Head[u]=cnt++; int n,m; int D_vis[maxn],D_num[maxn]; int source,meeting; bool bfs() memset(D_vis,0,sizeof(D_vis)); for(int i=0;i<=n;i++)//注意要覆盖所有点 D_num[i]=Head[i]; D_vis[source]=1; queue<int>q; q.push(source); int r=0; while(!q.empty()) int u=q.front(); q.pop(); int k=Head[u]; while(k!=-1) if(!D_vis[e[k].v]&&e[k].w) D_vis[e[k].v]=D_vis[u]+1; q.push(e[k].v); k=e[k].Next; return D_vis[meeting]; int dfs(int u,int f) if(u==meeting)return f; int &k=D_num[u]; while(k!=-1) if(D_vis[e[k].v]==D_vis[u]+1&&e[k].w) int d=dfs(e[k].v,min(f,e[k].w)); if(d>0) e[k].w-=d; e[k^1].w+=d; return d; k=e[k].Next; return 0; int Dinic() int ans=0; while(bfs()) int f; while((f=dfs(source,inf))>0) ans+=f; return ans; int main() // ios::sync_with_stdio(false); // freopen("in.txt", "r", stdin); while (scanf("%d%d",&n,&m)!=EOF&&n) memset(Head,-1,sizeof(Head)); cnt=0; source=1;meeting=2; for(int i=1;i<=m;i++) int x,y,z; scanf("%d%d%d",&x,&y,&z); add_edge(x,y,z); add_edge(y,x,z); Dinic(); bfs(); for(int i=1;i<=n;i++) D_vis[i]=min(D_vis[i],1); for(int i=0;i<cnt;i+=2) if(D_vis[e[i].u]!=D_vis[e[i].v]) printf("%d %d\n",e[i].u,e[i].v); printf("\n"); return 0;
以上是关于UVA - 10480 Sabotage (Dinic)的主要内容,如果未能解决你的问题,请参考以下文章