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

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

  1. hdu 5294 Tricks Device

    中规中矩的做法,第二发SAP.贴一发留恋. #include<cstdio> #include<cstring> #include<algorithm> #incl ...

  2. HDU 5294 Tricks Device(最短路+最大流)

    题意:给一个无向图(连通的),张在第n个点,吴在第1个点,'吴'只能通过最短路才能到达'张',两个问题:(1)张最少毁掉多少条边后,吴不可到达张(2)吴在张毁掉最多多少条边后仍能到达张. 思路:将所有 ...

  3. 【HDU - 3870】Catch the Theves(平面图转对偶图最短路,网络流最小割)

    题干: A group of thieves is approaching a museum in the country of zjsxzy,now they are in city A,and t ...

  4. Tricks Device 最短路+最大流

    http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1006&ojid=0&cid=12578&hide= ...

  5. HDU 3046 Pleasant sheep and big big wolf 最小割

    题意: 给定n*m个点的矩阵 0为空点.1为羊.2为狼 相邻点之间有一条路. 问要使得狼与羊不连通最少要去掉几条边 最小割 #include<stdio.h> #include<st ...

  6. HDU - 3987 Harry Potter and the Forbidden Forest(最小割最少边数)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的图,求最小割的最小边数 题目分析:和hdu6214大同小异,都是模板题,这个题目用第一种方法,也就是先跑一遍最大流,然后修改一下残余网络上的 ...

  7. 【HDU - 5889】Barricade(最短路+网络流,最小割)

    题干: The empire is under attack again. The general of empire is planning to defend his castle. The la ...

  8. 2018.10.12【BZOJ1319】【CEOI2008】oeder(最小割)

    传送门 解析: 最小割入门题. 思路: 我们考虑直接做出全部的任务,然后考虑我们可能获得的最小亏损,其中放弃一个任务也是一种亏损,租用或购买一个机器也是一种亏损. 我们这样建图,从源点向每个任务连容量 ...

  9. 学习手记(2018/7/14~2018/7/18)——快乐纪中

    2018/7/14:普通的纪中一天 儿子兄弟表示法 将一颗多叉树转换为二叉树的方法,左子节点连原树的第一个儿子,右子节点连原树的右边的兄弟 适用范围:树形dp 数位dp常见方法 状态压缩 分类讨论 记 ...

  10. 训练日志 2018.10.18

    花了一周时间把之前学过的算法和题重新看了一下,重新架构了一下知识体系,对 DP 和搜索有了更深刻的认识,这周正式开始图论内容,抽空再将搜索的优化和 A* 算法学习一下 2018.10.18

最新文章

  1. Firefox 46解决安全问题,改善性能
  2. java数组定义便利,java数组的定义(菜鸟教程)
  3. 赠书:京东当当新书榜TOP1的“算法小抄”!
  4. Android修改高度,android – 如何在运行时更改软键盘的高度?
  5. 系统学习深度学习(三十九)--基于模型的强化学习与Dyna算法框架
  6. Java快逸报表展现demo_快逸报表展示图片—来自本地/网络的图片
  7. Git 下载与安装教程
  8. JAVA打印中文乱码问题
  9. Android 点击图片全屏预览 -——ZoomPreviewPicture默认预览使用
  10. 11111122266666
  11. JS校验统一社会信用代码的真实性
  12. stc89c52rc转移到面包板,使用oled屏
  13. winrar正确破解方法
  14. 微信小程序项目实例——手势解锁
  15. APP移动应用测试策略与工具思维导图
  16. oop练习(第11周)
  17. 12-属性动画源码分析
  18. 深度粗排模型的GMV优化实践:基于全空间-子空间联合建模的蒸馏校准模型
  19. error: unable to unlink old 'antzb-web/src/main/webapp/js/ny-details.js': Invalid argument
  20. 2021年1月做算法题记录(Java实现)

热门文章

  1. 用算法判断输入的一个数是几位数
  2. 杨咩咩的编程求学之路之开篇
  3. java 熄灯问题_C++基础算法学习——熄灯问题
  4. 【小程序】小程序里跳转网页链接
  5. 数字科技陪伴企业成长|突破封锁,庚顿数据助力中国名牌全球瞩目
  6. 清华大学ISATAP访问IPv6设置
  7. 猜拳游戏 java_用java实现一个猜拳小游戏
  8. 在自己的网站上实现QQ授权登录
  9. 求职简历-机器学习工程师
  10. 用资源管理器打开ftp站点跳转浏览器解决方法