Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

貌似不是精度的问题

Posted by 619116104 at 2013-04-25 11:21:19 on Problem 3155
AC Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=105;
const int maxm=1005;
const int maxl=4*(maxn+maxm);
const int inf=1000000000;
const double zero=1e-10;

int n,m,L,S,T,ans;
int E[maxl],pre[maxl],A[maxm],B[maxm];
int fst[maxn],d[maxn],q[maxn],dist[maxn],vis[maxn],V[maxn]; 
double w[maxl];

void add(int a,int b,double c)
{
     E[L]=b; w[L]=c; pre[L]=fst[a]; fst[a]=L++;
     E[L]=a; w[L]=0; pre[L]=fst[b]; fst[b]=L++;
}

void init()
{
     memset(d,0,sizeof(d));
     scanf("%d%d",&n,&m);
     for(int i=1;i<=m;i++)
     {
         scanf("%d%d",&A[i],&B[i]);
         d[A[i]]++; d[B[i]]++;
     }
}

void create(double g)
{
     memset(fst,255,sizeof(fst));
     memset(pre,255,sizeof(pre));
     L=S=0; T=n+1;
     for(int i=1;i<=n;i++) add(S,i,m),add(i,T,m+2*g-d[i]);
     for(int i=1;i<=m;i++) add(A[i],B[i],1),add(B[i],A[i],1);
}

bool bfs()
{
     int head,tail;
     memset(q,0,sizeof(q));
     memset(dist,255,sizeof(dist));
     head=1; tail=2; q[1]=S; dist[S]=0;
     while(head<tail)
     {
           int u=q[head];
           for(int k=fst[u];k!=-1;k=pre[k]) 
               if(w[k]>zero && dist[E[k]]==-1) dist[E[k]]=dist[u]+1,q[tail++]=E[k];
           head++;
     }
     return(dist[T]!=-1);
}

double dfs(int u,double flow)
{
    double sum=0;
    if(u==T) return flow;
    for(int k=fst[u];k!=-1;k=pre[k])
        if(w[k]>zero && dist[E[k]]==dist[u]+1)
        {
           double tmp=dfs(E[k],min(flow,w[k]));
           w[k]-=tmp; w[k^1]+=tmp; sum+=tmp; flow-=tmp;
           if(flow<=zero) break;
        }
    return sum;
}

void find(int u)
{
     vis[u]=1;
     for(int k=fst[u];k!=-1;k=pre[k]) 
         if(w[k]>zero && !vis[E[k]]) V[++ans]=E[k],find(E[k]);
}

void work()
{
     double low=1.0/n,high=1.0*m,lim=1.0/(n*n)/10,maxflow;
     while(high-low>lim)
     {
           double mid=(low+high)/2; maxflow=0;;
           create(mid);
           while(bfs()) maxflow+=dfs(S,inf);
           if(m*n-maxflow>zero) low=mid; /*******/ else high=mid-lim;
     }
     create(low); maxflow=0; //********
     while(bfs()) maxflow+=dfs(S,inf);
     memset(vis,0,sizeof(vis)); 
     ans=0; find(S);
     if(ans==0) {printf("1\n1\n"); return;}
     printf("%d\n",ans); sort(V+1,V+ans+1);
     for(int i=1;i<=ans;i++) printf("%d\n",V[i]);
}

int main()
{
    init();
    work();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator