当前位置: 代码迷 >> 综合 >> 2018/7/18 HDU 5294 Tricks Device 最短路建图+最小割 训练日记2
  详细解决方案

2018/7/18 HDU 5294 Tricks Device 最短路建图+最小割 训练日记2

热度:28   发布时间:2023-11-23 07:30:46.0

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条隧道,每条隧道通过需要一个特定的时间。

吴邪在1点,张起灵在n点。吴邪需要走到n点,但是只能走这整个隧道的最短路。

问: 
张起灵最少需要将多少条隧道堵住,吴邪就走不到自己这里; 
吴邪在最多多少条隧道被堵住的情况下,依然能走到张起灵那里。

分析:

这题是一个最短路+网络流的模板综合题。

我们先需要先找一遍最短路,但是这个找最短路需要计算这些最短路里(因为有可能有多条)通过隧道数量最少的那一条。
然后将这条最短路重新建图,算出其最小割(最大流),这个值就是张起灵的答案。

吴邪的答案就是总共m条隧道减去那条被建图的最短路走过的隧道数。

扩展:
hdu 5889 Barricade

有n个点,m条无向边。敌人在点n想要攻击到点1,每次敌人一定走的都是最短路,如果我们不想要敌人从n出发走到点1,去掉每一条边都有对应的权值,求这个最小权值花费。

代码

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010  //点
#define MAXM 800010//边
#define inf 1000000000
using namespace std;
int N,M,s,t;
struct HeapNode //Dijkstra算法用到的优先队列的节点
{
    int d,u,len;
    HeapNode(int d,int u,int len):d(d),u(u),len(len){}
    bool operator < (const HeapNode &rhs)const
    {
        if(d!=rhs.d||u!=rhs.u)
        return d > rhs.d;
 
        return len<rhs.len;
    }
};
 
struct Edge     //边
{
    int from,to,dist;
    Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct Dijkstra
{
    int n,m;            //点数和边数,编号都从0开始
    vector<Edge> edges; //边列表
    vector<int> G[MAXN];//每个节点出发的边编号(从0开始编号)
    bool done[MAXN];    //是否已永久标号
    int d[MAXN];        //s到各个点的距离
    int road[MAXN];
 
void init(int n)
    {
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();//清空邻接表
        edges.clear();  //清空边列表
    }
 
    void AddEdge(int from,int to,int dist)
    {//如果是无向图,每条无向边调用两次AddEdge
        edges.push_back(Edge(from,to,dist) );
        m = edges.size();
        G[from].push_back(m-1);
    }
 
    void dijkstra(int s)//求s到所有点的距离
    {
        priority_queue<HeapNode> Q;
        for(int i=0;i<n;i++) d[i]=inf;
        d[s]=0;
        road[s]=0;
        memset(done,0,sizeof(done));
        Q.push(HeapNode(0,s,0) );
        while(!Q.empty())
        {
            HeapNode x=Q.top(); Q.pop();
            int u=x.u;
            if(done[u]) continue;
            done[u]= true;
 
            for(int i=0;i<G[u].size();i++)
            {
                Edge& e= edges[G[u][i]];
                if(d[e.to]> d[u]+e.dist)
                {
                    d[e.to] = d[u]+e.dist;
                    road[e.to]=road[u]+1;
                    Q.push(HeapNode(d[e.to],e.to,road[u]+1) );
                }
                else
                if(d[e.to]==d[u]+e.dist)
                {
                    if(road[u]+1<road[e.to]){
                    d[e.to] = d[u]+e.dist;
                    road[e.to]=road[u]+1;
                    Q.push(HeapNode(d[e.to],e.to,road[u]+1) );
                    }
                }
 
            }
        }
    }
}DJ;
 
struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM];
int tol;
int head[MAXN];
int dep[MAXN];
int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为y
 
//int n;//n是总的点的个数,包括源点和汇点
 
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
 
void addedge(int u,int v,int w)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}
void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int que[MAXN];
    int front,rear;
    front=rear=0;
    dep[end]=0;
    que[rear++]=end;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dep[v]!=-1)continue;
            que[rear++]=v;
            if(rear==MAXN)rear=0;
            dep[v]=dep[u]+1;
            ++gap[dep[v]];
        }
    }
}
int ISAP(int start,int end)
{
    int res=0;
    BFS(start,end);
    int cur[MAXN];
    int S[MAXN];
    int top=0;
    memcpy(cur,head,sizeof(head));
    int u=start;
    int i;
    while(dep[start]<N)
    {
        if(u==end)
        {
            int temp=inf;
            int inser;
            for(i=0;i<top;i++)
               if(temp>edge[S[i]].cap)
               {
                   temp=edge[S[i]].cap;
                   inser=i;
               }
            for(i=0;i<top;i++)
            {
                edge[S[i]].cap-=temp;
                edge[S[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=edge[S[top]].from;
        }
        if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路
          break;
        for(i=cur[u];i!=-1;i=edge[i].next)
           if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
             break;
        if(i!=-1)
        {
            cur[u]=i;
            S[top++]=i;
            u=edge[i].to;
        }
        else
        {
            int min=N;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(edge[i].cap==0)continue;
                if(min>dep[edge[i].to])
                {
                    min=dep[edge[i].to];
                    cur[u]=i;
                }
            }
            --gap[dep[u]];
            dep[u]=min+1;
            ++gap[dep[u]];
            if(u!=start)u=edge[S[--top]].from;
        }
    }
    return res;
}
int main()
{
    int i,j,x,y,z,ant;
    while(~scanf("%d%d",&N,&M))
    {
        DJ.init(N);
        for(i=0;i<M;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(x==y)continue;
            DJ.AddEdge(x-1,y-1,z);
            DJ.AddEdge(y-1,x-1,z);
        }
        DJ.dijkstra(0);
        ant=M-DJ.road[N-1];
        init();
        M=DJ.edges.size();
        for(i=0;i<M;i++)
        {
            x=DJ.edges[i].from;
            y=DJ.edges[i].to;
            z=DJ.edges[i].dist;
            if(DJ.d[y]==DJ.d[x]+z)
            addedge(x,y,1);
        }
        printf("%d %d\n",ISAP(0,N-1),ant);
    }
}

  相关解决方案