zoj 2676

来源:互联网 发布:vs2017 js智能提示 编辑:程序博客网 时间:2024/06/03 14:38

 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2676

Network Wars

 

Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the network are especially important --- they are connected to global world network and president palace network respectively.

The server connected to the president palace network has number 1, and the server connected to the global world network has numbern.

Recently the company Max Traffic has decided to take control over some cables so that it could see what data is transmitted by the president palace users. Of course they want to control such set of cables, that it is impossible to download any data from the global network to the president palace without transmitting it over at least one of the cables from the set.

To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has some cost. Since the company's main business is not spying, but providing internet connection to home users, its management wants to make the operation a good investment. So it wants to buy such a set of cables, that cablesmean cost} is minimal possible.

That is, if the company buys k cables of the total cost c, it wants to minimize the value ofc/k.

Input

There are several test cases in the input. The first line of each case containsn and m (2 <= n <= 100 , 1 <= m <= 400 ). Nextm lines describe cables~--- each cable is described with three integer numbers: servers it connects and the cost of the cable. Cost of each cable is positive and does not exceed107.

Any two servers are connected by at most one cable. No cable connects a server to itself. The network is guaranteed to be connected, it is possible to transmit data from any server to any other one.

There is an empty line between each cases.

Output

First output k --- the number of cables to buy. After that output the cables to buy themselves. Cables are numbered starting from one in order they are given in the input file. There should an empty line between each cases.

Example

InputOutput

6 81 2 31 3 32 4 22 5 23 4 23 5 25 6 34 6 3
43 4 5 6 
4 51 2 21 3 22 3 12 4 23 4 2
31 2 3

题意:一个无向图,求最小的平均割:,其中,wi表示边权,该表达式的意思就是选择某些边集构成S-T割,使得平均割最小。

分析:0-1分数规划问题,设,则令mincut=因为ci只能取0或1所以转换模型,边权值变为,每条边取与不取使得取到的边集成为一个最小S-T割,因为这个0-1分数规划满足单调性且当且仅当mincut=0时取到最小值,mincut<0时小于最优解,mincut>0时大于最优解,所以可以二分的值,使得min=0。这里对于的边一定是可取的,因为这些边只会减小最小割所以一定是可取的。最后二分的时候注意精度。

代码

#include<stdio.h>#include<string.h>#include<iostream>#include<cmath>#include<algorithm>using namespace std;#define ep 1e-7#define N 150#define M 1650int n,e,head[N],dep[N],que[N],cur[N];int e1,head1[N],nxt1[M],pnt1[M];double cost1[M];char vis[N],flag[M];struct node{int x,y;int nxt;double c;}edge[M];void addedge(int u,int v,double c){edge[e].x=u;edge[e].y=v;edge[e].nxt=head[u];edge[e].c=c;head[u]=e++;edge[e].x=v;edge[e].y=u;edge[e].nxt=head[v];edge[e].c=0;head[v]=e++;}void addedge1(int u,int v,double c){pnt1[e1]=v;nxt1[e1]=head1[u];cost1[e1]=c;head1[u]=e1++;pnt1[e1]=u;nxt1[e1]=head1[v];cost1[e1]=c;head1[v]=e1++;}double maxflow(int s,int t){int i,j,k,front,rear,top;double min,res=0;while(1){memset(dep,-1,sizeof(dep));front=0;rear=0;que[rear++]=s;dep[s]=0;while(front!=rear){i=que[front++];for(j=head[i];j!=-1;j=edge[j].nxt)if(fabs(edge[j].c)>ep&&dep[edge[j].y]==-1){dep[edge[j].y]=dep[i]+1;que[rear++]=edge[j].y;}}if(dep[t]==-1)break;memcpy(cur,head,sizeof(head)); for(i=s,top=0;;){if(i==t){min=1e20;for(k=0;k<top;k++)if(min>edge[que[k]].c){min=edge[que[k]].c;front=k;}for(k=0;k<top;k++){edge[que[k]].c-=min;edge[que[k]^1].c+=min;}res+=min;i=edge[que[top=front]].x;}for(j=cur[i];cur[i]!=-1;j=cur[i]=edge[cur[i]].nxt)if(dep[edge[j].y]==dep[i]+1&&fabs(edge[j].c)>ep)break;if(cur[i]!=-1){que[top++]=cur[i];i=edge[cur[i]].y;}else{if(top==0)break;dep[i]=-1;i=edge[que[--top]].x;}}}return res;}int bin(double left,double right)  //二分{double mid,sum;int i,j;while(left<right||fabs(left-right)<1e-5){mid=(left+right)/2.0;sum=0;memset(flag,0,sizeof(flag));e=0;memset(head,-1,sizeof(head));for(i=1;i<=n;i++)for(j=head1[i];j!=-1;j=nxt1[j])if(cost1[j]-mid<0&&!flag[j^1])//小于0的边一定取,所以不用连边{sum+=cost1[j]-mid;flag[j]=1;flag[j^1]=1;}else if(cost1[j]-mid>0)  //大于0建图addedge(i,pnt1[j],cost1[j]-mid);double val=maxflow(1,n);if(fabs(val+sum)<1e-5)return 1;else if(val+sum>0)left=mid+0.00001;else right=mid-0.00001;}return 0;}void dfs(int u) //确定割集{int i;vis[u]=1;for(i=head[u];i!=-1;i=edge[i].nxt){if(fabs(edge[i].c)>ep&&vis[edge[i].y]==0)dfs(edge[i].y);}}int main(){int m,i,j,u,v,num;double w,Max;while(scanf("%d%d",&n,&m)!=EOF){e1=0;memset(head1,-1,sizeof(head1));Max=0;for(i=0;i<m;i++){scanf("%d%d%lf",&u,&v,&w);addedge1(u,v,w);if(w>Max) Max=w;}memset(vis,0,sizeof(vis));bin(1.0,Max);dfs(1);for(i=1;i<=n;i++)for(j=head1[i];j!=-1;j=nxt1[j])if(vis[i]&&!vis[pnt1[j]])flag[j]=1;num=0;for(i=0;i<e1;i+=2)if(flag[i]||flag[i+1])num++;printf("%d\n",num);for(i=0;i<e1;i+=2)if(flag[i]||flag[i+1])printf("%d ",i/2+1);printf("\n");}return 0;}


 

原创粉丝点击