HDU 5294 Tricks Device(最短路+最小割)

来源:互联网 发布:玄空飞星软件下载 编辑:程序博客网 时间:2024/05/18 03:06

Tricks Device

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


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题意:给一个无向图,求出1->n的所有最短路图中的最小割和最小边数,第一个答案就是最小割,第二个答案就是m-最小边数。思路:比赛时只看题目,没时间做,隔天卡了将近6个小时。。先跑一遍最短路,然后重新建一个最短路的图,并把所有的边权都定为1,然后跑一遍最大流和最短路求最小割和最小边数。建新图应在跑完最短路后,我大多数时间都在跑最短路过程的更新建图,然后狂T了好几个小时。
#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <math.h>#include <string.h>#include <vector>#include <queue>#define inf 1<<30using namespace std;struct edge1{int to, cap, rev;};vector<edge1> g[2009];struct edge2{int to, cost;};typedef pair<int, int> P;vector<edge2> G[2009];vector<edge2> GG[2009];int d[2009];bool used[2009];int n;void add_edge(int from, int to, int cap){    g[from].push_back((edge1){to, cap, g[to].size()});    g[to].push_back((edge1){from, 0, g[from].size()-1});}int dfs(int v, int t, int f){    if(v==t) return f;    used[v]=true;    for(int i=0; i<g[v].size(); i++)    {        edge1 &e = g[v][i];        if(!used[e.to] && e.cap > 0)        {            int d=dfs(e.to, t, min(f, e.cap));            if(d>0)            {                e.cap-=d;                g[e.to][e.rev].cap+=d;                return d;            }        }    }    return 0;}int max_flow(int s, int t){    int flow=0;    for(;;)    {        memset(used, 0, sizeof(used));        int f=dfs(s, t, inf);        if(f==0)return flow;        flow+=f;    }}void dijkstra1(){    priority_queue<P, vector<P>, greater<P> > que;    fill(d, d+n+1, inf);    d[1]=0;    que.push(P(0, 1));    while(!que.empty())    {        P p=que.top();que.pop();        int v=p.second;        if(d[v]<p.first) continue;        for(int i=0; i<G[v].size(); i++)        {            edge2 e=G[v][i];            if(d[e.to]>=d[v]+e.cost)            {                d[e.to]=d[v]+e.cost;                que.push(P(d[e.to], e.to));            }        }    }}void dijkstra2(){    priority_queue<P, vector<P>, greater<P> > que;    fill(d, d+n+1, inf);    d[1]=0;    que.push(P(0, 1));    while(!que.empty())    {        P p=que.top();que.pop();        int v=p.second;        if(d[v]<p.first) continue;        for(int i=0; i<GG[v].size(); i++)        {            edge2 e=GG[v][i];            if(d[e.to]>=d[v]+e.cost)            {                d[e.to]=d[v]+e.cost;                que.push(P(d[e.to], e.to));            }        }    }}void build(){    for(int i=0; i<=n; i++)    {        for(int j=0; j<G[i].size(); j++)        {            if(d[i]+G[i][j].cost==d[G[i][j].to])            {                GG[i].push_back((edge2){G[i][j].to, 1});                add_edge(i, G[i][j].to, 1);            }        }    }}int main(){    int m;    while(scanf("%d%d", &n, &m)!=EOF)    {        for(int i=0; i<=n; i++)        {            g[i].clear();            G[i].clear();            GG[i].clear();        }        for(int i=0; i<m; i++)        {            int a, b, co;            scanf("%d%d%d", &a, &b, &co);            G[a].push_back((edge2){b, co});            G[b].push_back((edge2){a, co});        }        dijkstra1();        build();        dijkstra2();        printf("%d %d\n", max_flow(1, n), m-d[n]);    }    return 0;}


0 0
原创粉丝点击