HDU 3251 Being a Hero 網路流 最小割

來源:互聯網
上載者:User
/*題意:給你f個可選城市,每個城市都有其價值w0,國王的城市在1,現在國王不想見到你(國王不想通過某種路徑到達你選定的城市)——將你選的若干城市隔離出去,每條道路隔斷都需要花費w1,現在問你可以達到的最大價值並要求輸出你割斷的路的編號題解:網路流模型題,將所有可選點連入超級匯點,求出最小割(等於最大流),最大價值為所有可選點的價值和減去最小割我們預設最小割靠近源點的為左側,靠近匯點為右側所有的割邊的求法:最後一次求增廣路的過程中,匯點不可達,此時從源點可增流的點都在記錄路徑的數組Qu裡面枚舉數組裡的值x,求出以x為起點,終點不在Qu中的所有邊,這些邊就是組成最小割的邊*/#include <stdio.h>#include <time.h>#include <algorithm>#include <map>#include <string.h>#include<queue>#include<iostream>#define N 60010using namespace std;const int maxn=1009;const int inf=1<<30;int n,m,f;//**************************************************  //為dinic求最大流模版  struct edge  {       int v, next;       int val;   } net[ 200010 ];   int Qnum;  //記錄左側點的數量int level[maxn], Qu[maxn], out[maxn],next[maxn],ins[maxn],first[maxn];  class Dinic {   public:       int end;  int now;int low,high;    Dinic() {           end = 0;           memset( next, -1, sizeof(next) );       }       inline void insert( int x, int y, int c) {           net[end].v = y, net[end].val = c,          net[end].next = next[x],           next[x] = end ++;           net[end].v = x, net[end].val = 0,          net[end].next = next[y],           next[y] = end ++;       }       bool BFS( int S, int E ) {           memset( level, -1, sizeof(level) );           low = 0, high = 1;           Qu[0] = S, level[S] = 0;           for( ; low < high; ) {               int x = Qu[low];               for( int i = next[x]; i != -1; i = net[i].next ) {                   if( net[i].val == 0 ) continue;                   int y = net[i].v;                   if( level[y] == -1 ) {                       level[y] = level[x] + 1;                       Qu[ high ++] = y;                   }               }               low ++;           }           return level[E] != -1;       }            int MaxFlow( int S, int E ){           int maxflow = 0;           for( ; BFS(S, E) ; ) {               memcpy( out, next, sizeof(out) );               now = -1;               for( ;; ) {                   if( now < 0 ) {                       int cur = out[S];                       for(; cur != -1 ; cur = net[cur].next )                            if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )                               break;                       if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;                       else break;                   }                   int u = net[ Qu[now] ].v;                   if( u == E ) {                       int flow = inf;                       int index = -1;                       for( int i = 0; i <= now; i ++ ) {                           if( flow > net[ Qu[i] ].val )                               flow = net[ Qu[i] ].val, index = i;                       }                       maxflow += flow;                       for( int i = 0; i <= now; i ++ )                           net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;                       for( int i = 0; i <= now; i ++ ) {                           if( net[ Qu[i] ].val == 0 ) {                               now = index - 1;                               break;                           }                       }                   }                   else{                       int cur = out[u];                       for(; cur != -1; cur = net[cur].next )                            if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])                               break;                       if( cur != -1 )                           Qu[++ now] = cur, out[u] = net[cur].next;                       else out[u] = -1, now --;                   }               }           }   Qnum=high;//一定要記錄此時在Qu數組裡點的個數,方便後面枚舉        return maxflow;       }   };     struct E{int v,next;}edg[200011];int ans;void init(int k){int u,v,w;scanf("%d%d%d",&n,&m,&f);memset(ins,0,sizeof(ins));memset(first,-1,sizeof(first));Dinic my;int edg_num=1;for(int i=1;i<=m;i++){scanf("%d%d%d",&u,&v,&w);edg[i].v=v;edg[i].next=first[u];first[u]=i;my.insert(u,v,w);}int sum=0;for(int i=0;i<f;i++){scanf("%d%d",&u,&w);my.insert(u,n+1,w);sum+=w;}printf("Case %d: %d\n",k,sum-my.MaxFlow(1,n+1));sum=0;for(int i=0;i<Qnum;i++)ins[Qu[i]]=1;//標記所有左側的點ans=0;//dfs(1);for(int i=0;i<Qnum;i++){int x=Qu[i];for(int j=first[x];j!=-1;j=edg[j].next){if(!ins[edg[j].v])out[ans++]=j;}}printf("%d",ans);for(int i=0;i<ans;i++){printf(" %d",out[i]);}printf("\n");}int main() {    int Case;scanf("%d",&Case);for(int i=1;i<=Case;i++){init(i);//solve();}    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.