zoj2676(网络流+分数规划)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/83245707

题意:求一个割集使得1和n之间不连通,且割集的边权与边数目之比最大

比值问题显然需要分数规划,设二分枚举的分数为t,那么就把原题的边权改成v-t来跑最小割判断正负就行了。。

考虑到网络流只接受正边权,我们把负边权先去掉再去跑最小割,而显然负边权是一定要取的(因为只要让最终结果小于0),所以跑完最小割再把负边权加上然后去判正负就可以了。。。

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 105
#define nm 2005
#define pi 3.1415926535897931
const int inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





struct edge{int t;double v;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM],*tmp[NM];
void _add(int x,int y,double v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
void add(int x,int y,double v){_add(x,y,v);_add(y,x,0);h[x]->rev=h[y];h[y]->rev=h[x];}
struct tmp{int x,y,v;}a[nm];
int n,m,d[NM],cnt[NM],tot,c[nm];
double ans;
bool v[NM];


double maxflow(){
    double flow=0;edge*j;double s=inf;
    mem(d);mem(cnt);mem(p);mem(tmp);
    inc(i,1,n)tmp[i]=h[i];
    cnt[0]=tot;
    for(int x=1;d[x]<tot;){
	for(j=tmp[x];j;j=j->next)if(j->v>eps&&d[x]==d[j->t]+1)break;
	if(j){
	    s=min(s,j->v);p[j->t]=tmp[x]=j;
	    if((x=j->t)==n){
		for(;p[x];x=p[x]->rev->t)p[x]->v-=s,p[x]->rev->v+=s;
		flow+=s;s=inf;
	    }
	}else{
	    if(!--cnt[d[x]])break;d[x]=tot;
	    link(x)if(j->v>eps&&d[x]>d[j->t]+1)tmp[x]=j,d[x]=d[j->t]+1;
	    cnt[d[x]]++;
	    if(p[x])x=p[x]->rev->t;
	}
    }
    //printf("%lf ",flow);
    return flow;
}


void dfs(int x){
    v[x]++;
    link(x)if(!v[j->t]&&j->v>eps)dfs(j->t);
}

bool check(double t){
    double s=0;
    mem(e);mem(h);o=e;
    inc(i,1,m)if(a[i].v>t)add(a[i].x,a[i].y,a[i].v-t),add(a[i].y,a[i].x,a[i].v-t);else s+=t-a[i].v; 
    //printf("%lf\n",t);
    return maxflow()+eps<s;
}

int main(){
    bool f=false;
    while(~scanf("%d%d",&n,&m)){
	if(f)putchar('\n');f=true;
	tot=n;ans=inf;
	inc(i,1,m)a[i].x=read(),a[i].y=read(),a[i].v=read();
	for(double x=0,y=20000000,cnt=0;cnt<=100;cnt++)if(check(mid))ans=mid,y=mid;else x=mid;
	check(ans);tot=0;mem(v);
	//printf("%lf\n",ans);
	dfs(1);
	inc(i,1,m)if(a[i].v+eps<ans||v[a[i].x]!=v[a[i].y])c[++tot]=i;
	sort(c+1,c+1+tot);
	printf("%d\n",tot);
	inc(i,1,tot-1)printf("%d ",c[i]);printf("%d\n",c[tot]);
    }
    return 0;
}

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

Source: Andrew Stankevich's Contest #8

Submit    Status


猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/83245707
今日推荐