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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值