【floyd+拆點+二分答案+網路流】POJ 2391

來源:互聯網
上載者:User

#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <string>#include <cstdio>#include <math.h>#include <iomanip>#include <cstdlib>#include <limits.h>#include <string.h>#include <iostream>#include <fstream>#include <algorithm>using namespace std;#define LL long long#define pii pair<int ,int>#define bug cout<<"here!!"<<endl#define PI acos(-1.0)#define FRE freopen("input.txt","r",stdin)#define FF  freopen("output.txt","w",stdout)#define eps 1e-8#define MIN INT_MINconst LL MAX = (1ll<<58);const int inf = (1<<29);#define N 410#define M (N*N)int n,m;//n為點數 m為邊數int h[N];int gap[N];int p[N],ecnt;int source,sink;struct edge{    int v;    int next;    int val;}e[M];inline void init(){memset(p,-1,sizeof(p));ecnt=0;}//有向inline void insert1(int from,int to,int val){    e[ecnt].v=to;    e[ecnt].val=val;    e[ecnt].next=p[from];    p[from]=ecnt++;    swap(from,to);    e[ecnt].v=to;    e[ecnt].val=0;    e[ecnt].next=p[from];    p[from]=ecnt++;}//無向inline void insert2(int from,int to,int val){    e[ecnt].v=to;    e[ecnt].val=val;    e[ecnt].next=p[from];    p[from]=ecnt++;    swap(from,to);    e[ecnt].v=to;    e[ecnt].val=val;    e[ecnt].next=p[from];    p[from]=ecnt++;}inline int dfs(int pos,int cost){    if (pos==sink){        return cost;    }    int j,minh=n-1,lv=cost,d;    for (j=p[pos];j!=-1;j=e[j].next){        int v=e[j].v,val=e[j].val;        if(val>0){            if (h[v]+1==h[pos]){                if (lv<e[j].val) d=lv;                else d=e[j].val;                d=dfs(v,d);                e[j].val-=d;                e[j^1].val+=d;                lv-=d;                if (h[source]>=n) return cost-lv;                if (lv==0) break;            }            if (h[v]<minh)   minh=h[v];        }    }    if (lv==cost){        --gap[h[pos]];        if (gap[h[pos]]==0) h[source]=n;        h[pos]=minh+1;        ++gap[h[pos]];    }    return cost-lv;}int sap(int st,int ed){    source=st;    sink=ed;    int ans=0;    memset(gap,0,sizeof(gap));    memset(h,0,sizeof(h));    gap[st]=n;    while (h[st]<n){        ans+=dfs(st,INT_MAX);    }    return ans;}LL g[205][205];int flow;int x[205],y[205];void floyd(int nn){    int i,j,k;    for(k=1;k<=nn;k++){        for(i=1;i<=nn;i++){            if(g[i][k] < MAX)            for(j=1;j<=nn;j++){                if(g[k][j] < MAX)                if(g[i][k] + g[k][j] < g[i][j]){                    g[i][j] = g[i][k] + g[k][j];                }            }        }    }}bool chk(LL mid,int nn){    int i,j;    init();    for(i=1;i<=nn;i++){        insert1(i,i+nn,inf);        insert1(0,i,x[i]);        insert1(i+nn,n-1,y[i]);    }    for(i=1;i<nn;i++){        for(j=i+1;j<=nn;j++){            if(g[i][j] <= mid){                insert1(i,j+nn,inf);                insert1(j,i+nn,inf);            }        }    }    int ans = sap(0,n-1);    return ans == flow;}LL l,r,mid;void gao(int nn){    l = 0;    LL ans = -1;    while(l<=r){        mid = (l+r)>>1;        if(chk(mid,nn)){            r = mid-1;            ans = mid;        } else l = mid+1;    }    if(ans==-1)puts("-1");    else    printf("%I64d\n",ans);}int main(){FRE;    while(scanf("%d%d",&n,&m) != -1){        int i,j;        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                g[i][j] = MAX;            }            g[i][i] = 0;        }        int nn = n;        n = 2*n+2;        flow = 0;        r=0;        for(i=1;i<=nn;i++){            scanf("%d%d",&x[i],&y[i]);            flow += x[i];        }        while(m--){            int a,b;            LL c;            scanf("%d%d%I64d",&a,&b,&c);            if(g[a][b] > c) {            g[a][b] = g[b][a] = c;            r+=c;            }        }        floyd(nn);        gao(nn);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.