SPOJ 13041 網路流+二分

來源:互聯網
上載者:User
SPOJ Problem Set (classical)13041. The Black RidersProblem code: AMR12A

 

'Hush!' said Frodo. 'I think I hear hoofs again.'They stopped suddenly and stood as silent as tree-shadows, listening. There was a sound of hoofs in the lane, some way behind, but coming slow and clear down the wind. Quickly and quietly they slipped off the path, and ran into the deeper shade under the oak-trees.The hoofs drew nearer. They had no time to find any hiding-place better than the general darkness under the trees. - Frodo, Sam and Pippin, when they encounter a Black Rider.Indeed, the Black Riders are in the Shire, and they are looking for the One Ring. There are N hobbits out in their fields, but when they hear the Riders approaching, or feel the fear cast by their presence, they immediately wish to run and hide in M holes located
nearby.Now, each hole has space for just 1 hobbit; however, once a hobbit reaches a hole, he is able to make room for one more hobbit by digging away at the earth. The time required to make enough space for a second hobbit is C units. Also, each hole CANNOT hold more
than 2 hobbits, even after digging. Also note that a hobbit can begin making space for the next hobbit only after he reaches the hole.You are given the time required to travel from each hobbit's current location to each hole. Find the minimum amount of time it will take before at least K of the hobbits are hiding safely.Input (STDIN):The first line contains T, the number of test cases.The first line of each test case contains 4 integers - N (no of hobbits), M (no of holes), K (minimum number of hobbits to hide) and C (time taken to expand a hole).The next N lines contain M integers each, denoting the time taken for each hobbit to each hole.Output (STDOUT):Output one line per test case which contains the minimum time. Constraints:1 <= T <= 61 <= N, M <= 1001 <= K <= min(N, 2 * M)0 < C < 10,000,0000 < Time taken by the hobbits to the holes < 10,000,000Time Limit: 2sMemory Limit: 64MBSample Input:23 3 2 109 11 132 10 1412 15 124 3 3 81 10 1001 10 100100 100 612 10 10Sample Output:109Notes/Explanation of Sample Input:For the first test case, there are 3 hobbits and 3 holes, and we need to get atleast 2 of them to safety. We can send the first hobbit to the first hole, and the second hobbit to the second hole, thereby taking 10 time units.For the second test case, we can make hobbit #1 reach hole 1 at time 1, hobbit #2 reach hole 1 at time 9 (by when hobbit #1 would have finished digging the hole), and hobbit #3 reach hole 3 at time 6.

'Hush!' said Frodo. 'I think I hear hoofs again.'

They stopped suddenly and stood as silent as tree-shadows, listening. There was a sound of hoofs in the lane, some way behind, but coming slow and clear down the wind. Quickly and quietly they slipped off the path, and ran into the deeper shade under the oak-trees.

The hoofs drew nearer. They had no time to find any hiding-place better than the general darkness under the trees. 

- Frodo, Sam and Pippin, when they encounter a Black Rider.

 

Indeed, the Black Riders are in the Shire, and they are looking for the One Ring. There are N hobbits out in their fields, but when they hear the Riders approaching, or feel the fear cast by their presence, they immediately wish to run and hide in M holes located
nearby.

 

Now, each hole has space for just 1 hobbit; however, once a hobbit reaches a hole, he is able to make room for one more hobbit by digging away at the earth. The time required to make enough space for a second hobbit is C units. Also, each hole CANNOT hold more
than 2 hobbits, even after digging. Also note that a hobbit can begin making space for the next hobbit only after he reaches the hole.

 

You are given the time required to travel from each hobbit's current location to each hole. Find the minimum amount of time it will take before at least K of the hobbits are hiding safely.

 

Input (STDIN):

 

The first line contains T, the number of test cases.

The first line of each test case contains 4 integers - N (no of hobbits), M (no of holes), K (minimum number of hobbits to hide) and C (time taken to expand a hole).

The next N lines contain M integers each, denoting the time taken for each hobbit to each hole.

 

Output (STDOUT):

 

Output one line per test case which contains the minimum time. 

 

Constraints:

1 <= T <= 6

1 <= N, M <= 100

1 <= K <= min(N, 2 * M)

0 < C < 10,000,000

0 < Time taken by the hobbits to the holes < 10,000,000

 

Sample Input:

2

3 3 2 10

9 11 13

2 10 14

12 15 12

4 3 3 8

1 10 100

1 10 100

100 100 6

12 10 10

 

Sample Output:

10

9

很巧妙的二分額~~~沒想到啊!!!

#include <cstring>#include <cstdio>#include <queue>#define MAXN 40000#define MAXM 400000#define inf 0x3f3f3f3fusing namespace std;struct node{    int u,v,f,c;};node e[MAXM];int first[MAXN],next[MAXM];int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];int cc;inline void add_edge(int u,int v,int f,int c){    e[cc].u=u;    e[cc].v=v;    e[cc].f=f;    e[cc].c=c;    next[cc]=first[u];    first[u]=cc;    cc++;    e[cc].u=v;    e[cc].v=u;    e[cc].f=0;    e[cc].c=0;    next[cc]=first[v];    first[v]=cc;    cc++;}int ISAP(int s,int t,int n){    int cur_flow,flow_ans=0,u,tmp,neck,i,v;    memset(d,0,sizeof(d));    memset(gap,0,sizeof(gap));    memset(pre,-1,sizeof(pre));    for(i=0;i<=n;i++)        curedge[i]=first[i];    gap[0]=n+1;    u=s;    while(d[s]<=n)    {        if(u==t)        {            cur_flow=inf;            for(i=s;i!=t;i=e[curedge[i]].v)            {                if(cur_flow>e[curedge[i]].f)                {                    neck=i;                    cur_flow=e[curedge[i]].f;                }            }            for(i=s;i!=t;i=e[curedge[i]].v)            {                tmp=curedge[i];                e[tmp].f-=cur_flow;                e[tmp^1].f+=cur_flow;            }            flow_ans+=cur_flow;            u=neck;        }        for(i=curedge[u];i!=-1;i=next[i])        {            v=e[i].v;            if(e[i].f&&d[u]==d[v]+1)                break;        }        if(i!=-1)        {            curedge[u]=i;            pre[v]=u;            u=v;        }        else        {            if(0==--gap[d[u]])                break;            curedge[u]=first[u];            for(tmp=n+5,i=first[u];i!=-1;i=next[i])                if(e[i].f)                    tmp=min(tmp,d[e[i].v]);            d[u]=tmp+1;            ++gap[d[u]];            if(u!=s)                u=pre[u];        }    }    return flow_ans;}void build(int limit){    int i;    for(i=0;i<cc;i=i+2)    {        e[i].f=(e[i].c<=limit);        e[i^1].f=0;    }}int main(){    int tt;    scanf("%d",&tt);    while(tt--)    {        memset(first,-1,sizeof(first));        memset(next,-1,sizeof(next));        cc=0;        int n,m,k,c;        scanf("%d%d%d%d",&n,&m,&k,&c);        int i,j;        int s=0;        int t=n+m+m+1;        for(i=1;i<=n;i++)        {            add_edge(s,i,1,0);            for(j=1;j<=m;j++)            {                int x;                scanf("%d",&x);                add_edge(i,n+j,1,x);                add_edge(i,n+j+m,1,x+c);            }        }        for(i=1;i<=m;i++)        {            add_edge(n+i,t,1,0);            add_edge(n+i+m,t,1,0);        }        int l=0,mid;        int r=20000010;        int ans=inf;        while(l<=r)        {            mid=(l+r)>>1;            build(mid);            int res=ISAP(s,t,t);            if(res>=k)            {                if(res==k)                {                    ans=min(ans,mid);                }                r=mid-1;            }            else if(res<k)                l=mid+1;        }        printf("%d\n",ans);    }    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.