hdu5294

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). 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 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
 

題目大意:n個點m條無向邊,假設從起點0到終點n-1的最短路距離爲dist,求最少刪除多少條邊使得
                圖中不再存在最短路,最多刪除多少條邊使得圖中 仍然存在最短路。

思路:最大流+spfa
          利用spfa求出從1到n的最短路,選擇最短路建圖,使得每條邊的權值爲1,從1到n走一遍最大流
          就可以求出答案。  詳情見代碼解釋。
#include<stdio.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXM = 2e3+5;
const int MAXN = 12e4+5;
int n,m;
int head[MAXN],len;
int dep[MAXM],w[MAXM];
bool visit[MAXM];
struct edge{
    int en,wor,next;
}e[MAXN];
void add(int a,int b,int c)  //第一次建圖
{
    e[len].en=b;
    e[len].wor=c;
    e[len].next=head[a];
    head[a]=len++;

    e[len].en=a;
    e[len].wor=c;
    e[len].next=head[b];
    head[b]=len++;
}
void spfa(int s)       //spfa記錄最短路徑
{
    int front=0,rear=0,q[MAXM];
    memset(visit,0,sizeof(visit));
    memset(dep,INF,sizeof(dep));
    memset(w,INF,sizeof(w));
    visit[s]=1;
    q[rear++]=s;
    dep[s]=0;
    w[s]=0;
    while(front<rear){
        int k=q[front++];
        visit[k]=0;
        for(int l=head[k];l!=-1;l=e[l].next){
            int g=e[l].en,f=e[l].wor;
            if(w[g]==w[k]+f){
                dep[g]=min(dep[g],dep[k]+1);
                if(!visit[g]){
                    visit[g]=1;
                    q[rear++]=g;
                }
            }
            else if(w[g]>w[k]+f){
                dep[g]=dep[k]+1;
                w[g]=w[k]+f;
                if(!visit[g]){
                    visit[g]=1;
                    q[rear++]=g;
                }
            }
        }
    }

<span style="font-size: 14.3999996185303px;">struct Eg{                  
    int to,flow,rev;
    Eg(int a,int b,int c){
        to=a,flow=b,rev=c;
    }
};
vector<Eg>G[MAXM];</span><span style="font-size:14px;">      </span><span style="font-size: 14.3999996185303px;">
void add2(int a,int b,int c)      //重新建圖
{
    //printf("%d %d %d\n",a,b,c);
    G[a].push_back(Eg(b,c,G[b].size()));
    //最後一個數存的是當前b的序號
    G[b].push_back(Eg(a,0,G[a].size()-1));
    //最後一個數存的是當前a的序號
}
void build()
{
    for(int i=1;i<=n;i++){
        for(int j=head[i];j!=-1;j=e[j].next){
            int t1=e[j].en,t2=e[j].wor;
            if(w[t1]-w[i]==t2){        //***很重要的一個方式  利用此公式可以找出所有的最短路徑
                add2(i,t1,1);
            }
        }
    }
}
int dfs(int s,int t,int f)
{
    if(s==t) return f;
    visit[s]=1;
    for(int i=0;i<G[s].size();i++){
        Eg &e=G[s][i];
        if(!visit[e.to]&&e.flow>0){
            int d=dfs(e.to,t,min(e.flow,f)); //深搜繼續查找最小流量
            if(d>0){
                e.flow-=d;
                G[e.to][e.rev].flow+=d; //更新流量
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1){
        memset(visit,0,sizeof(visit));
        int f=dfs(s,t,INF);  //查找每一次的最小流量
        if(f==0)       //f=0 說明當前已經不存在通路
            return flow;
        flow+=f;
    }
    return 0;
}
void init()
{
    memset(head,-1,sizeof(head));
    len=0;
    for(int i=0;i<=n;i++)
        G[i].clear();
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        for(int i=1;i<=m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        spfa(1);
        build();
        int maxflow=max_flow(1,n);
        printf("%d %d\n",maxflow,m-dep[n]);
    }
}
</span>

以下爲最大流的模板(時間更快縮小3倍)
int Q[MAXM];
int dep[MAXN],cur[MAXN],sta[MAXN];

bool bfs(int s,int t,int n)
{
    int front=0,tail=0;
    memset(dep,-1,sizeof(dep[0])*(n+1));
    dep[s]=0;
    Q[tail++]=s;
    while (front<tail)
    {
        int u=Q[front++];
        for (int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if (edge[i].cap>edge[i].flow && dep[v]==-1)
            {
                dep[v]=dep[u]+1;
                if (v==t) return true;
                Q[tail++]=v;
            }
        }
    }
    return false;
}

int Maxflow(int s,int t,int n)
{
    int maxflow=0;
    while (bfs(s,t,n))
    {
        for (int i=0;i<n;i++) cur[i]=head[i];
        int u=s,tail=0;
        while (cur[s]!=-1)
        {
            if (u==t)
            {
                int tp=INF;
                for (int i=tail-1;i>=0;i--)
                    tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
                maxflow+=tp;
                for (int i=tail-1;i>=0;i--)
                {
                    edge[sta[i]].flow+=tp;
                    edge[sta[i]^1].flow-=tp;
                    if (edge[sta[i]].cap-edge[sta[i]].flow==0)
                        tail=i;
                }
                u=edge[sta[tail]^1].to;
            }
            else if (cur[u]!=-1 && edge[cur[u]].cap > edge[cur[u]].flow &&dep[u]+1==dep[edge[cur[u]].to])
            {
                sta[tail++]=cur[u];
                u=edge[cur[u]].to;
            }
            else
            {
                while (u!=s && cur[u]==-1)
                    u=edge[sta[--tail]^1].to;
                cur[u]=edge[cur[u]].next;
            }
        }
    }
    return maxflow;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章