hdu 1839:Delay Constrained Maximum Capacity Path

來源:互聯網
上載者:User

1點是珍貴礦場,N點是礦物加工廠,現在需要把礦物從1點運到N點,礦物在離開礦場T時間後就會變質,礦場與工廠之間有通道串連,每個通道有運輸時間和最大運送量,求出從礦場到工廠運送時間不超過時間T的最大運送量。主意,一條道路上的最大運送量取決於它經過通路上的最小運送量。


二分+spfa過。知道每一段路的運送量,按從大到小進行排列,然後對這個排列進行二分,每次二分取中間的運送量作為限制,然後用時間作為距離進行spfa。spfa時用之前的那個限制作為最小運送量,鬆弛時只鬆弛那些運送量大於這個限制的邊,這樣就可以保證這個限制就是當前通路的最大運送量。然後判斷此時N點是否連通或者N點的時間是否大於給定的時間T。如此迴圈直到二分完畢。


代碼:

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <iostream>using namespace std;struct Edge{    int to;    int cap;    int time;    int next;};const int inf=0x3f3f3f3f;int n,m,T,limit;int first[10005];Edge edge[50005*2];int cap[50005];int dis[10005];bool had[10005];queue <int> q;void spfa(){    memset(dis,0x3f,sizeof(dis));    memset(had,0,sizeof(had));    dis[1]=0;    q.push(1);    while(!q.empty()){        int t=q.front(); q.pop();        had[t]=0;        for(int i=first[t];i!=-1;i=edge[i].next){            if(dis[edge[i].to]>dis[t]+edge[i].time && edge[i].cap>=limit){                dis[edge[i].to]=dis[t]+edge[i].time;                if(!had[edge[i].to]){                    had[edge[i].to]=1;                    q.push(edge[i].to);                }            }        }    }}int main(){    //freopen("in.txt","r",stdin);    int Case; cin>>Case;    while(Case--){        cin>>n>>m>>T;        memset(first,-1,sizeof(first));        int e=0;        for(int i=0;i<m;i++){            int a,b,c,d;            cin>>a>>b>>c>>d;            edge[e].to=b;            edge[e].cap=c;            edge[e].time=d;            edge[e].next=first[a];            first[a]=e++;            edge[e].to=a;            edge[e].cap=c;            edge[e].time=d;            edge[e].next=first[b];            first[b]=e++;            cap[i]=c;        }        sort(cap,cap+m,greater<int>());        int mid,l=0,r=m-1;        while(l<=r){            mid=(l+r)/2;            limit=cap[mid];            spfa();            if(dis[n]==inf || dis[n]>T)                l=mid+1;            else                r=mid-1;        }        cout<<cap[l]<<endl;    }    return 0;}



然後就是不知道為什麼,sort開始到二分結束那段改成下面這段代碼就出問題了。先留著,以後再看。

        sort(cap,cap+m);        int mid,l=0,r=m-1;        while(l<=r){            mid=(l+r)/2;            limit=cap[mid];            spfa();            if(dis[n]==inf || dis[n]>T)                r=mid-1;            else                l=mid+1;        }        cout<<limit<<endl;


聯繫我們

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