程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4940 無源匯有上下界最大流

hdu 4940 無源匯有上下界最大流

編輯:C++入門知識

hdu 4940 無源匯有上下界最大流


/*
\
題意:給出一個有向強連通圖,每條邊有兩個值分別是破壞該邊的代價和把該邊建成無向邊的代價(建立無向邊的前提是刪除該邊)問是否存在一個集合S,和一個集合的補集T,破壞所有S集合到T集合的邊代價和是X,然後修復T到S的邊為無向邊代價和是Y,滿足Yvc3Bhbj4KIMjnufu05tTav8nQ0MH3ICDEx8O0y7XD97bU09rIztLitcQgUyC8r7rPwfez9rXEv8+2qLXI09ogwffI67XEo6wgwfez9rXEvMbL47XEIFggv8+2qNCh09q1yNPa1eK49sH3wb+jqFjKx8/CvefWrrrNo6mjrCC8xsvjs/bAtLXEWSCjqMnPvefWrrrNo6m/z7aotPPT2rXI09og1eK49sH3wb8gIL/PtqjC+tfjWDw9WaGjCiovCiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPHN0cmluZy5oPgojaW5jbHVkZTxxdWV1ZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBOIDMwMAojZGVmaW5lIGluZiAweDNmZmZmZmZmCnN0cnVjdCBub2RlIHsKICAgaW50IHUsdix3LG5leHQ7Cn1iaWFuW04qTiozXTsKaW50IGhlYWRbTl0seW9uZyxkaXNbTl0sd29ya1tOXTsKdm9pZCBpbml0KCl7Cnlvbmc9MDsKbWVtc2V0KGhlYWQsLTEsc2l6ZW9mKGhlYWQpKTsKfQp2b2lkIGFkZGJpYW4oaW50IHUsaW50IHYsaW50IHcpIHsKYmlhblt5b25nXS51PXU7CmJpYW5beW9uZ10udj12OwpiaWFuW3lvbmddLnc9dzsKYmlhblt5b25nXS5uZXh0PWhlYWRbdV07CmhlYWRbdV09eW9uZysrOwp9CnZvaWQgYWRkKGludCB1LGludCB2LGludCB3KSB7CmFkZGJpYW4odSx2LHcpOwphZGRiaWFuKHYsdSwwKTsKfQppbnQgbWluKGludCBhLGludCBiKQp7CiAgICByZXR1cm4gYTxiP2E6YjsKfQppbnQgYmZzKGludCBzLGludCB0KQp7CiAgICBtZW1zZXQoZGlzLC0xLHNpemVvZihkaXMpKTsKICAgIHF1ZXVlPGludD5xOwogICAgcS5wdXNoKHMpOwogICAgZGlzW3NdPTA7CiAgICB3aGlsZSghcS5lbXB0eSgpKQogICAgewogICAgICAgIGludCB1PXEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGZvcihpbnQgaT1oZWFkW3VdO2khPS0xO2k9YmlhbltpXS5uZXh0KQogICAgICAgIHsKICAgICAgICAgICAgaW50IHY9YmlhbltpXS52OwogICAgICAgICAgICBpZihiaWFuW2ldLncmYW1wOyZhbXA7ZGlzW3ZdPT0tMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZGlzW3ZdPWRpc1t1XSsxOwogICAgICAgICAgICAgICAgcS5wdXNoKHYpOwogICAgICAgICAgICAgICAgaWYodj09dCkKICAgICAgICAgICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CmludCBkZnMoaW50ICBzLGludCBsaW1pdCxpbnQgdCkKewogICAgaWYocz09dClyZXR1cm4gbGltaXQ7CiAgICBmb3IoaW50ICZhbXA7aT13b3JrW3NdO2khPS0xO2k9YmlhbltpXS5uZXh0KQogICAgewogICAgICAgIGludCB2PWJpYW5baV0udjsKICAgICAgICBpZihiaWFuW2ldLncmYW1wOyZhbXA7ZGlzW3ZdPT1kaXNbc10rMSkKICAgICAgICB7CiAgICAgICAgICAgIGludCB0dD1kZnModixtaW4obGltaXQsYmlhbltpXS53KSx0KTsKICAgICAgICAgICAgaWYodHQpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGJpYW5baV0udy09dHQ7CiAgICAgICAgICAgICAgICBiaWFuW2leMV0udys9dHQ7CiAgICAgICAgICAgICAgICByZXR1cm4gdHQ7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQppbnQgZGluaWMoaW50IHMsaW50IHQpCnsKICAgIGludCBhbnM9MDsKICAgIHdoaWxlKGJmcyhzLHQpKQogICAgewogICAgICAgIG1lbWNweSh3b3JrLGhlYWQsc2l6ZW9mKGhlYWQpKTsKICAgICAgICB3aGlsZShpbnQgdHQ9ZGZzKHMsaW5mLHQpKQogICAgICAgICAgICBhbnMrPXR0OwogICAgfQogICAgcmV0dXJuIGFuczsKfQppbnQgbWFpbigpewogICAgICAgIGludCBzdW0sYSxiLGMsVCxkLGFucyxpLGs9MCxuLG0sdCxTLHdbTl07CiAgICAgICAgc2NhbmYo"%d",&t);
        while(t--) {
                init();
            scanf("%d%d",&n,&m);
        S=0;T=n+1;
        memset(w,0,sizeof(w));
            for(i=1;i<=m;i++) {
                scanf("%d%d%d%d",&a,&b,&c,&d);
                add(a,b,d);
               w[b]+=c;
               w[a]-=c;
            }
            sum=0;
            for(i=1;i<=n;i++) {
                if(w[i]>0) {
                    sum+=w[i];
                    add(S,i,w[i]);
                }
                if(w[i]<0)
                    add(i,T,-w[i]);
            }
            ans=dinic(S,T);
            if(sum==ans)
                printf("Case #%d: happy\n",++k);
            else
                printf("Case #%d: unhappy\n",++k);
        }

return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved