ZOJ 2676 Network Wars(01分数规划-二分+最小割)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676

题意:给出一个无向图,每个边有权值w,起点s和终点t。找出一个割集S,使得sigama(w)/|S|最小?

思路:二分答案x,则每条边的权值为w'=w-x,求最大流。最大流大于等于0,则说明x偏小。否则偏大。另外,对于w'为负的边,直接加入答案。最后从源点DFS一次。

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))

#define MP(x,y) make_pair(x,y)

#define EPS 1e-5



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(int &x,int &y,int &z,int &t){scanf("%d%d%d%d",&x,&y,&z,&t);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}





void PR(int x) {printf("%d\n",x);}

void PR(int x,int y) {printf("%d %d\n",x,y);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.5lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}





const int INF=1000000000;

const int N=100005;



struct Node

{

	int v,next;

	double cap,flow;

};





Node edges[N];

int n,m,s,t,head[N],e;

int num[N],h[N],curedge[N],pre[N];





void add(int u,int v,double cap)

{

    edges[e].v=v;

    edges[e].cap=cap;

    edges[e].flow=0;

    edges[e].next=head[u];

    head[u]=e++;

}



void Add(int u,int v,double cap)

{

    add(u,v,cap);

    add(v,u,0);

}





double Maxflow(int s,int t,int n)

{

	double ans=0.0,d;

	int i,k,x,u;



	memset(num,0,sizeof(num));

	memset(h,0,sizeof(h));

	for(i=0;i<=n;i++) curedge[i]=head[i];

	num[n]=n;u=s;

	while(h[u]<n)

	{

		if(u==t)

		{

			d=INF+1;

			for(i=s;i!=t;i=edges[curedge[i]].v) if(d>edges[curedge[i]].cap)

				k=i,d=edges[curedge[i]].cap;

			for(i=s;i!=t;i=edges[curedge[i]].v)

			{

				x=curedge[i];

				edges[x].cap-=d;

				edges[x].flow+=d;

				edges[x^1].cap+=d;

				edges[x^1].flow-=d;

			}

			ans+=d;u=k;

		}

		for(i=curedge[u];i!=-1;i=edges[i].next) if(edges[i].cap>0&&h[u]==h[edges[i].v]+1)

			break;

		if(i!=-1)

		{

			curedge[u]=i;

			pre[edges[i].v]=u;

			u=edges[i].v;

		}

		else

		{

			if(--num[h[u]]==0) break;

			curedge[u]=head[u];

			for(x=n,i=head[u];i!=-1;i=edges[i].next) if(edges[i].cap>0&&h[edges[i].v]<x)

				x=h[edges[i].v];

			h[u]=x+1;num[h[u]]++;

			if(u!=s) u=pre[u];

		}

	}

	return ans;

}





int visit[10005],a[405],b[405];

double c[405];



int OK(double mid)

{

    clr(head,-1); e=0;

	double sum=0.0;

	int i;

	s=1,t=n;

	FOR1(i,m)

	{

		if(c[i]<=mid) sum+=c[i]-mid;

		else

		{

			Add(a[i],b[i],c[i]-mid);

			Add(b[i],a[i],c[i]-mid);

		}

	}

	sum+=Maxflow(s,t,t);

	return sum>=0;

}







void DFS(int u)

{

	visit[u]=1;

	for(int i=head[u];i!=-1;i=edges[i].next)

	{

		if(visit[edges[i].v]||edges[i].cap<=0) continue;

		DFS(edges[i].v);

	}

}



int main()

{

	while(scanf("%d%d",&n,&m)!=-1)

	{

		int i;

		double low=INF,high=1.0,mid;

		FOR1(i,m)

		{

		    RD(a[i],b[i]); RD(c[i]);

            low=min(low,c[i]);

			high=max(high,c[i]);

		}

		while(high-low>EPS)

		{

			mid=(low+high)/2;

			if(OK(mid)) low=mid;

			else high=mid;

		}

		clr(visit,0); DFS(1);

		int ans=0;

		for(i=1;i<=m;i++) if(c[i]<=mid||visit[a[i]]+visit[b[i]]==1)

        {

            ans++;

        }

        PR(ans);

		int flag=0;

		for(i=1;i<=m;i++) if(c[i]<=mid||visit[a[i]]+visit[b[i]]==1)

		{

			if(flag) putchar(' ');

			else flag=1;

			printf("%d",i);

		}

		puts("");

	}

	return 0;

}

  

你可能感兴趣的:(NetWork)