当前位置: 首页 > news >正文

HDU(2485),最小割最大流

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485

Destroying the bus stations

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2651    Accepted Submission(s): 891


Problem Description
Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is destroying some bus stations to make the army can’t get to the airport in k minutes. It takes exactly one minute for a bus to pass any road. All bus stations are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station.
No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station.


Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.
 

 

Input
There are several test cases. Input ends with three zeros.

For each test case:

The first line contains 3 integers, n, m and k. (0< n <=50, 0< m<=4000, 0 < k < 1000)
Then m lines follows. Each line contains 2 integers, s and f, indicating that there is a road from station No. s to station No. f.
 
Output
For each test case, output the minimum number of stations Gabiluso must destroy.
 
Sample Input
5 7 3 1 3 3 4 4 5 1 2 2 5 1 4 4 5 0 0 0
 
Sample Output
2
 
Source
2008 Asia Regional Beijing
 
题意:

给定n个点, m条有向边 ,k

下面m条有向边

问删最少几个点使得1-n的最短路>k

分析:
其证明还没看懂,先做了再说咯。证明在紫书370,写一下结论:在增广路算法结束时,f是s-t最大流,(S,T)是最小割。
然后问了一下阳哥,记录几个结论,最大流=最小割(边)=最小割(点)。
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 4000 + 10;
int k;

struct Edge
{
    int from,to,cap,flow,cost;
    Edge() {}
    Edge(int a,int b,int c,int d,int e):from(a),to(b),cap(c),flow(d),cost(e) {}
};

struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> g[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[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 cap,int cost)
    {
        Edge e1= Edge(from,to,cap,0,cost), e2= Edge(to,from,0,0,-cost);
        edges.push_back(e1);
        edges.push_back(e2);
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool spfa(int s,int t, int & flow,int & cost)
    {
        for(int i=0; i<n; i++)
            d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0;
        inq[s]=1;
        p[s]=0;
        a[s]=INF;
        queue<int>q;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            inq[u]=0;
            for(int i=0; i<g[u].size(); i++)
            {
                Edge & e = edges[g[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=g[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to])
                    {
                        q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        if(d[t]>k)
            return false;
        if(d[t]==INF)
            return false;

        flow+=a[t];
        cost+=a[t]*d[t];
        for(int u=t; u!=s; u=edges[p[u]].from)
        {
            edges[p[u]].flow +=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }

    int  MincostMaxflow(int s,int t)
    {
        int flow=0,cost =0;
        while(spfa(s,t,flow,cost));
        return flow;
    }
} sol;


int main()
{
    freopen("input.txt","r",stdin);
    int n,m;
    while(scanf("%d%d%d",&n,&m,&k))
    {
        int s = 0,t = 2*n+1;
        if(n==0&&m==0&&k==0) break;
        int u,v;
        sol.init(n*2+2);
        for(int i=1; i<=n; i++)
            sol.addedge(i+n,i,1,0);

        sol.addedge(1,1+n,INF,0);
        sol.addedge(n,2*n,INF,0);
        sol.addedge(0,1,INF,0);
        sol.addedge(2*n,t,INF,0);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            sol.addedge(u,v+n,INF,1);
        }
        printf("%d\n",sol.MincostMaxflow(s,t));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/TreeDream/p/5806126.html

相关文章:

  • iOS 对模型对象进行归档
  • TopN算法与排行榜
  • Servlet 生命周期、工作原理
  • POJ 2375
  • 关于SQL镜像配置报错
  • 共享库
  • Oracle 函数返回表实例2种写法实例
  • 重走java--Step 2
  • 高并发系统之队列术
  • servlet 开发出错原因分析
  • java--集合框架
  • 粗浅看Struts2和Hibernate框架
  • Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
  • jquery内容选择器(匹配包含指定选择器的元素)
  • Unity3D Shader入门指南(转)
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • 【附node操作实例】redis简明入门系列—字符串类型
  • C语言笔记(第一章:C语言编程)
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • Hibernate【inverse和cascade属性】知识要点
  • Java面向对象及其三大特征
  • Java-详解HashMap
  • jQuery(一)
  • js继承的实现方法
  • python大佬养成计划----difflib模块
  • Python实现BT种子转化为磁力链接【实战】
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • 动态规划入门(以爬楼梯为例)
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 如何实现 font-size 的响应式
  • 学习Vue.js的五个小例子
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • puppet连载22:define用法
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • #NOIP 2014# day.2 T2 寻找道路
  • #pragma 指令
  • (11)MATLAB PCA+SVM 人脸识别
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (Ruby)Ubuntu12.04安装Rails环境
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (力扣)循环队列的实现与详解(C语言)
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • .net Application的目录
  • .NET gRPC 和RESTful简单对比
  • .NET 依赖注入和配置系统
  • .net6 webapi log4net完整配置使用流程