您的位置:首页 > 理论基础 > 计算机网络

hdu 5294 - Tricks Device(2015 Multi-University Training Contest 1 )最短路+网络流

2015-07-27 10:47 579 查看

Tricks Device

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\leq2000), M(\leq60000). 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≤n1\leq ai,bi\leq n), it costs li(0<li≤1000) 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 9

1 2 2

2 3 2

2 4 1

3 5 3

4 5 4

5 8 1

1 6 2

6 7 5

7 8 1

Sample Output

2 6

题意:最少删除多少条边破坏最短路,最多删除多少条边不破坏最短路

思路:对于第一问,我们可以求出最短路,并保存路径,然后求一边最小割就是答案了,对于第二问,也很简单,只需要用最短路总的边数减去最短路中边数最少的一条就可以了。

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=2010;
const int maxm=100010;
const int INF=0x3f3f3f3f;
int N,M;
int head[maxn],tot,nn;
int pre[maxn],dis[maxn];
bool vis[maxn];
int gap[maxn],cur[maxn];
vector<int> G[maxn];
struct node
{
    int v,next,f;
}edge[maxm*2];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(pre,-1,sizeof(pre));
    for(int i=0;i<=N;i++)G[i].clear();
}
void add_edge(int u,int v,int f)
{
    edge[tot].v=v;
    edge[tot].next=head[u];
    edge[tot].f=f;
    head[u]=tot++;
}
void SPFA(int st)
{
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        vis[i]=0;
    }
    queue<int> q;
    q.push(st);
    vis[st]=1;
    dis[st]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].f;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                G[v].clear();
                G[v].push_back(u);
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
            else if(dis[v]==dis[u]+w)
            {
                G[v].push_back(u);
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
void add_edge1(int x,int y,int f)
{
    edge[tot].v=y;
    edge[tot].f=f;
    edge[tot].next=head[x];
    head[x]=tot++;
    edge[tot].v=x;
    edge[tot].f=0;
    edge[tot].next=head[y];
    head[y]=tot++;
}
int SAP(int st,int en)
{
    for(int i=0;i<=nn;i++)
    {
        cur[i]=head[i];
        dis[i]=gap[i]=0;
    }
    int u=0;
    int flow=0,aug=INF;
    gap[st]=nn;
    u=pre[st]=st;
    bool flag;
    while(dis[st]<nn)
    {
        flag=0;
        for(int &j=cur[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].f>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].f<aug)aug=edge[j].f;
                pre[v]=u;
                u=v;
                if(u==en)
                {
                    flow+=aug;
                    while(u!=st)
                    {
                        u=pre[u];
                        edge[cur[u]].f-=aug;
                        edge[cur[u]^1].f+=aug;
                    }
                    aug=INF;
                }
                break;
            }
        }

        if(flag)continue;
        int mindis=nn;
        for(int j=head[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(dis[v]<mindis&&edge[j].f>0)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
int num[maxn];
int dfs(int u){
    if(u==1) return 0;
    if(num[u]!=INF) return num[u];//必须用数组存下来,这样保证边不会被重复加
    int len=G[u].size();
    for(int i=0;i<len;i++){
        int fa=G[u][i];
        add_edge1(G[u][i],u,1);
        add_edge1(u,G[u][i],1);
        num[u]=min(num[u],dfs(fa)+1);
    }
    return num[u];
}
int main()
{
    int a,b,c;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        init();
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
        }
        SPFA(1);
        tot=0;
        memset(head,-1,sizeof(head));
        memset(num,INF,sizeof(num));
        int ans=dfs(N);
        nn=N+1;
        printf("%d %d\n",SAP(1,N),M-ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: