Thieves—-簡單最小割

來源:互聯網
上載者:User

題目:http://acm.hdu.edu.cn/showproblem.php?pid=3491

註:拆點

原始碼:

#include <stdio.h>#include <string.h>#include <algorithm>#define inf 1e9using namespace  std;typedef int typec;const int N = 220, M = 40000;int n,m,s,t,u,v,cas,ans;int a[N];struct FlowNetwork {int e, head[N], d[N], vd[N], pre[N], cur[N];int nxt[M], eu[M], ev[M];typec c[M];void init() { e = 0; memset(head,-1,sizeof(head)); }void addedge(int u, int v, typec w) {eu[e]=u; ev[e]=v; c[e]=w; nxt[e]=head[u]; head[u]=e++;eu[e]=v; ev[e]=u; c[e]=0; nxt[e]=head[v]; head[v]=e++;//無向圖中第二個c[e]=w;}typec sap(int s, int t, int n){int i, u;memset(d,0,sizeof(d)); memset(vd,0,sizeof(vd));vd[0] = n;cur[u = s] = head[s];pre[s] = -1;typec temp, ans = 0;while (d[s] < n) {if(u == t){for(temp = inf, i = pre[u]; ~i; i = pre[eu[i]])temp = min(temp, c[i]);for(i = pre[u]; ~i; i = pre[eu[i]]){c[i] -= temp; c[i ^ 1] += temp;}ans += temp;  u = s;}for (i = cur[u]; ~i; i = nxt[i])//if (c[i] > eps && d[u] == d[ev[i]] + 1){if (c[i] > 0 && d[u] == d[ev[i]] + 1){cur[u] = i;//當前弧最佳化pre[u = ev[i]] = i;break;}if(i == -1){cur[u] = head[u];if (--vd[d[u]] == 0)break;vd[++d[u]]++;if(u != s)u = eu[pre[u]];}}return ans;}};int solve(){    FlowNetwork tmp;    tmp.init();    memset(a,0,sizeof(a));    for(int i=1;i<=n;i++)      {          scanf("%d",&a[i]);          if(i!=s&&i!=t)          tmp.addedge(i,i+n,a[i]);      }      tmp.addedge(s,s+n,inf);      tmp.addedge(t,t+n,0);    for(int i=0;i<m;i++)    {        scanf("%d %d",&u,&v);        tmp.addedge(u+n,v,inf);        tmp.addedge(v+n,u,inf);    }    return tmp.sap(s,t,2*n);}int main(){    //freopen("D:\\a.txt","r",stdin);    scanf("%d",&cas);    while(cas--)    {        scanf("%d %d %d %d",&n,&m,&s,&t);        ans=solve();        printf("%d\n",ans);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.