HDU4289:Tricks Device(最小割 & 拆点)

来源:互联网 发布:addiction动作数据mmd 编辑:程序博客网 时间:2024/06/14 08:08

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3864    Accepted Submission(s): 1090


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
8 91 2 22 3 22 4 13 5 34 5 45 8 11 6 26 7 57 8 1
 

Sample Output
2 6
 

Author
FZUACM
 

Source
2015 Multi-University Training Contest 1
题意:N个城市,警察要在某些城市据守使恐怖分子不能达到目标城市,每个城市有据守的花费,问保证达到目的下花费的最小值。

思路:最小割,因为权值在点上,将点拆成边即可,然后套最大流。

# include <iostream># include <cstdio># include <cstring>using namespace std;const int maxn = 2e5;const int INF = 0x7fffffff;int n, m, s, t, cnt=0, Next[1000], dis[1000], q[maxn];struct node{int v, w, next;}edge[maxn];void add_edge(int u, int v, int w){    edge[cnt] = {v, w, Next[u]};    Next[u] = cnt++;    edge[cnt] = {u, 0, Next[v]};    Next[v] = cnt++;}bool bfs(){    memset(dis, -1, sizeof(dis));    int l=0, r=0;    q[r++] = 0;    dis[0] = 0;    while(l<r)    {        int u = q[l++];        for(int i=Next[u]; i!=-1; i=edge[i].next)        {            int v = edge[i].v, w = edge[i].w;            if(dis[v]==-1 && w)            {                dis[v] = dis[u]+1;                q[r++] = v;            }        }    }    return dis[988] != -1;}int dfs(int u, int pre){    if(u == 988) return pre;    int ans = 0, f = 0;    for(int i=Next[u]; i!=-1; i=edge[i].next)    {        int v = edge[i].v, w = edge[i].w;        if(dis[v]==dis[u]+1 && w>0 && (f=dfs(v, min(pre, w))))        {            edge[i].w -= f;            edge[i^1].w += f;            ans += f;            pre -= f;            if(!pre)                break;        }    }    if(ans)        return ans;    dis[u] = -1;    return 0;}int main(){    while(~scanf("%d%d",&n,&m))    {        cnt = 0;        scanf("%d%d",&s,&t);        memset(Next, -1, sizeof(Next));        add_edge(0, s, INF), add_edge(t+n, 988, INF);        for(int i=1; i<=n; ++i)        {            int w;            scanf("%d",&w);            add_edge(i, i+n, w);        }        for(int i=0; i<m; ++i)        {            int a, b;            scanf("%d%d",&a,&b);            add_edge(a+n, b, INF);            add_edge(b+n, a, INF);        }        int max_flow = 0;        while(bfs()) max_flow += dfs(0, INF);        printf("%d\n",max_flow);    }    return 0;}