首页 > 代码库 > poj1966枚举源汇点 求最小点割DInic
poj1966枚举源汇点 求最小点割DInic
Cable TV Network
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4854 | Accepted: 2241 |
Description
The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor f of a network with n relays is:
1. n, if the net remains connected regardless the number of relays removed from the net.
2. The minimal number of relays that disconnect the network when removed.
For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.
1. n, if the net remains connected regardless the number of relays removed from the net.
2. The minimal number of relays that disconnect the network when removed.
For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.
Input
Write a program that reads several data sets from the standard input and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two integers: 0<=n<=50,the number of relays in the net, and m, the number of cables in the net. Follow m data pairs (u,v), u < v, where u and v are relay identifiers (integers in the range 0..n-1). The pair (u,v) designates the cable that interconnects the relays u and v. The pairs may occur in any order.Except the (u,v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net.
Sample Input
0 0 1 0 3 3 (0,1) (0,2) (1,2) 2 0 5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)
Sample Output
0 1 3 0 2
Hint
The first data set encodes an empty network, the second data set corresponds to a network with a single relay, and the following three data sets encode the nets shown in figure 1.
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=1000000000; int head[111],dis[111],tot,st,ed,n,m; struct node { int to,next,w; } e[10086]; struct tc { int a,b; } po[10086]; void add(int u,int v,int w) { e[tot].to=v; e[tot].next=head[u]; e[tot].w=w; head[u]=tot++; } bool bfs() { queue<int>Q; memset(dis,-1,sizeof(dis)); Q.push(st); dis[st]=0; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; ~i; i=e[i].next) { int v=e[i].to; if(dis[v]==-1&&e[i].w>0) { dis[v]=dis[u]+1; if(v==ed) return true; Q.push(v); } } } return false; } int dfs(int s,int low) { if(s==ed||low==0) return low; int ans=low,a; for(int i=head[s]; i+1; i=e[i].next) { int v=e[i].to; if(e[i].w>0&&dis[v]==dis[s]+1&&(a=dfs(v,min(e[i].w,ans)))) { ans-=a; e[i].w-=a; e[i^1].w+=a; if(!ans) return low; } } if(ans==low) dis[s]=-1; return low-ans; } void build() { memset(head,-1,sizeof(head)); tot=0; for(int i=1; i<=m; ++i) { add(po[i].a+n,po[i].b,INF); add(po[i].b+n,po[i].a,INF); } for(int i=0; i<n; ++i) add(i,i+n,1),add(i+n,i,0); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int a,b; for(int i=1; i<=m; ++i) { scanf(" (%d,%d)",&a,&b); po[i].a=a; po[i].b=b; } int ans=INF; for(int i=0; i<n; ++i) for(int j=i+1; j<n; ++j) { st=i+n; ed=j; int ct=0; build(); while(bfs()) ct+=dfs(st,INF); ans=min(ct,ans); } if(ans==INF) printf("%d\n",n); else printf("%d\n",ans); } }
poj1966枚举源汇点 求最小点割DInic
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。