01分数规划zoj2676(最优比例,最小割集+二分)

时间:2021-05-14 20:39:35
ZOJ Problem Set - 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 number n.

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 cables mean cost} is minimal possible.

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

Input

There are several test cases in the input. The first line of each case contains n and m (2 <= n <= 100 , 1 <= m <= 400 ). Next m 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 exceed 107.

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

Input Output
6 8
1 2 3
1 3 3
2 4 2
2 5 2
3 4 2
3 5 2
5 6 3
4 6 3
4
3 4 5 6
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2
3
1 2 3

 题意:给出一个网络连通图n个服务器m条网线以及费用,现在需要控制某些网线,令1发出的信号无论如何都不能到达n,且保证选择的网线总费用与网线总条数的比值最小,问需要选择的网线条数,并给出它们的序号;

分析:设比值r=c/k=sigma(wi*xi)/sigma(xi),设最优值为R,即r>=R
即:sigma(wi*xi)/sigma(xi)>=R
即:sigma(wi-R)*xi>=0,所以要二分枚举R值,若wi-R的权值小于0,则一定要选择,并且选择最小的割集中的边,当存在某个R使得sigma(wi-R)*xi==0时,R就是最优解
然后从1dfs一遍,把容量不为零的边走一遍,并把所有的点标记,最后搜索一遍编号,最后的答案就是权值为负值的边和割集中的边
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"iostream"
#include"queue"
#include"stack"
#include"map"
#include"string"
#define M 409
#define inf 0x3f3f3f3f
#define eps 1e-6
using namespace std;
struct node
{
int u,v,next;
double w;
}edge[M*10];
int t,head[M],work[M],a[M],b[M],c[M],dis[M],belong[M],use[M];
double min(double a,double b)
{
return a<b?a:b;
}
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,double w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int bfs(int S,int T)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
dis[S]=0;
q.push(S);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&dis[v]==-1)
{
dis[v]=dis[u]+1;
q.push(v);
if(v==T)
return 1;
}
}
}
return 0;
}
double dfs(int cur,double a,int T)
{
if(cur==T)return a;
for(int &i=work[cur];~i;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&dis[v]==dis[cur]+1)
{
double tt=dfs(v,min(a,edge[i].w),T);
if(tt)
{
edge[i].w-=tt;
edge[i^1].w+=tt;
return tt;
}
}
}
return 0;
}
double Dinic(int S,int T)
{
double ans=0;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
while(double tt=dfs(S,inf,T))
ans+=tt;
}
return ans;
}
double fun(int n,int m,double r)
{
init();
double sum=0;
for(int i=1;i<=m;i++)
{
if(c[i]>r)
{
add(a[i],b[i],c[i]-r);
add(b[i],a[i],c[i]-r);
}
else
sum+=c[i]-r;
}
return sum+Dinic(1,n);
}
void DFS(int u)
{
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w>eps&&!use[v])
DFS(v);
}
}
int main()
{
int n,m,kk=0;
while(scanf("%d%d",&n,&m)!=-1)
{
double l=0,r=0,mid;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
r+=c[i];
}
while(r-l>eps)
{
mid=(l+r)/2;
double msg=fun(n,m,mid);
if(msg>eps)
{
l=mid;
}
else
r=mid;
}
fun(n,m,mid);//重新跑一遍网络流,因为最后一次的网络流不一定是最优值mid的网络流
memset(use,0,sizeof(use));
DFS(1);
int num=0;
for(int i=1;i<=m;i++)
{
if(use[a[i]]!=use[b[i]]||c[i]<mid)
belong[num++]=i;
}
printf("%d\n",num);
printf("%d",belong[0]);
for(int i=1;i<num;i++)
printf(" %d",belong[i]);
printf("\n");
if(kk)
printf("\n");
kk++;
}
return 0;
}