poj 2175 Evacuation Plan(最小费用流+消圈)

poj 2175(费用流判负环+消环) 题意:给出n栋房子位置和每栋房子里面的人数,m个避难所位置和每个避难所可容纳人数。然后给出一个方案,判断该方案是否最优,如果不是求出一个更优的方案。 思路:很容易想到用最小费用流求出最优时间,在与原方案花费时间对比判断原方案是否最优。但是这种方法会超时的。 放弃该思路。 看看题目没要求要最优解,而是得到一个更优的解,那么如果在原图中能够找到一个总费用为负的回路的话,那就该解不是最优解,把该负环 阅读详情
Evacuation Plan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2963 Accepted: 791 Special Judge

Description

The City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in case of a nuclear war. Each fallout shelter has a limited capacity in terms of a number of people it can accommodate, and there's almost no excess capacity in The City's fallout shelters. Ideally, all workers from a given municipal building shall run to the nearest fallout shelter. However, this will lead to overcrowding of some fallout shelters, while others will be half-empty at the same time.

To address this problem, The City Council has developed a special evacuation plan. Instead of assigning every worker to a fallout shelter individually (which will be a huge amount of information to keep), they allocated fallout shelters to municipal buildings, listing the number of workers from every building that shall use a given fallout shelter, and left the task of individual assignments to the buildings' management. The plan takes into account a number of workers in every building - all of them are assigned to fallout shelters, and a limited capacity of each fallout shelter - every fallout shelter is assigned to no more workers then it can accommodate, though some fallout shelters may be not used completely.

The City Council claims that their evacuation plan is optimal, in the sense that it minimizes the total time to reach fallout shelters for all workers in The City, which is the sum for all workers of the time to go from the worker's municipal building to the fallout shelter assigned to this worker.

The City Mayor, well known for his constant confrontation with The City Council, does not buy their claim and hires you as an independent consultant to verify the evacuation plan. Your task is to either ensure that the evacuation plan is indeed optimal, or to prove otherwise by presenting another evacuation plan with the smaller total time to reach fallout shelters, thus clearly exposing The City Council's incompetence.

During initial requirements gathering phase of your project, you have found that The City is represented by a rectangular grid. The location of municipal buildings and fallout shelters is specified by two integer numbers and the time to go between municipal building at the location (Xi, Yi) and the fallout shelter at the location (Pj, Qj) is Di,j = |Xi - Pj| + |Yi - Qj| + 1 minutes.

Input

The input consists of The City description and the evacuation plan description. The first line of the input file consists of two numbers N and M separated by a space. N (1 ≤ N ≤ 100) is a number of municipal buildings in The City (all municipal buildings are numbered from 1 to N). M (1 ≤ M ≤ 100) is a number of fallout shelters in The City (all fallout shelters are numbered from 1 to M).

The following N lines describe municipal buildings. Each line contains there integer numbers Xi, Yi, and Bi separated by spaces, where Xi, Yi (-1000 ≤ Xi, Yi ≤ 1000) are the coordinates of the building, and Bi (1 ≤ Bi ≤ 1000) is the number of workers in this building.

The description of municipal buildings is followed by M lines that describe fallout shelters. Each line contains three integer numbers Pj, Qj, and Cj separated by spaces, where Pi, Qi (-1000 ≤ Pj, Qj ≤ 1000) are the coordinates of the fallout shelter, and Cj (1 ≤ Cj ≤ 1000) is the capacity of this shelter.

The description of The City Council's evacuation plan follows on the next N lines. Each line represents an evacuation plan for a single building (in the order they are given in The City description). The evacuation plan of ith municipal building consists of M integer numbers Ei,j separated by spaces. Ei,j (0 ≤ Ei,j ≤ 1000) is a number of workers that shall evacuate from the ith municipal building to the jth fallout shelter.

The plan in the input file is guaranteed to be valid. Namely, it calls for an evacuation of the exact number of workers that are actually working in any given municipal building according to The City description and does not exceed the capacity of any given fallout shelter.

Output

If The City Council's plan is optimal, then write to the output the single word OPTIMAL. Otherwise, write the word SUBOPTIMAL on the first line, followed by N lines that describe your plan in the same format as in the input file. Your plan need not be optimal itself, but must be valid and better than The City Council's one.

Sample Input

3 4
-3 3 5
-2 -2 6
2 2 5
-1 1 3
1 1 4
-2 -2 7
0 -1 3
3 1 1 0
0 0 6 0
0 3 0 2

Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1

题意:有n座建筑物,m个避难所,每座建筑物都有若干个人,每个避难所只能容纳有限的人,每座建筑物与每个避难所都有各自的坐标,点与点之间的距离为曼哈顿距离+1。要使所有建筑物的所有人都能到避难所去,且所有人所走的距离总和最小,题目已给出一个方案,即给出了每座建筑物有各多少个人到每个避难所,现在要你判断这种方案是不是最优的,若不是,则输出一种更优的方案(不一定要最优)。
思路:这题不必求最小费用流,但要用到消圈算法。
定理:一个费用流是最小费用流的充要条件是这个费用流的残量网络没有负费用圈。
         由这个定理,我们只需判断当前方案是否有负费用圈,没有即意味着当前方案为最优方案。
         如果有的话,此时只需沿着负费用圈把各边流量增加1,增流之后残量网络对应的方案肯定是一个更优方案(很容易证明)。
        
        
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 1000;
struct node{
    int x, y;
}a[maxn], b[maxn];
struct Edge{
    int u, v, cost, cap, flow ,next;
}et[maxn * maxn];
int cnt[maxn], pre[maxn], dis[maxn], eh[maxn];
int G[maxn][maxn], cap[maxn], peo[maxn], plan[maxn][maxn], flow[maxn];
bool vis[maxn];
int s, t, n, m, num;
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cost, int cap, int flow){
    Edge e = {u, v, cost, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap, int flow){
    add(u, v, cost, cap, flow);
    add(v, u, -cost, 0, -flow);
}
int getdis(node p, node q){
    return abs(p.x - q.x) + abs(p.y - q.y) + 1;
}
void build(){
    init();
    t = n + m + 1;
    for(int i = 1; i <= m; i++) addedge(i + n, t, 0, cap[i], flow[i]);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
    addedge(i, j + n, G[i][j], INF, plan[i][j]);
}
bool spfa(int s, int n, int nv){
    queue<int> Q;
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    memset(cnt, 0, sizeof(cnt));
    fill(&dis[0], &dis[maxn], INF);
    dis[s] = 0, vis[s] = true, cnt[s]++;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = eh[u]; i != -1; i = et[i].next)
        {
            int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
            if(cap - flow && dis[v] > dis[u] + cost)
            {
                dis[v] = dis[u] + cost;
                pre[v] = u;
                if(!vis[v])
                {
                    vis[v] = true;
                    Q.push(v);
                    cnt[v]++;
                    if(cnt[v] > n)        //有负环
                    {
                        memset(vis, false, sizeof(vis));
                         u = v;                    //点不一定在负环上
                         while(!vis[u])            //找到负环上任意一个点
                         {
                             vis[u] = true;
                             u = pre[u];
                         }
                         int end = u;        
                         u = pre[v = u];
                         do{                         //更改流量
                             if(u < v && v != t) plan[u][v - nv]++;
                             else if(u > v && u != t) plan[v][u - nv]--;
                             u = pre[v = u];
                         }while(v != end);
                         return false;
                    }
                }
            }
        }
    }
    return true;
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].y, &peo[i]);
        for(int i = 1; i <= m; i++) scanf("%d%d%d", &b[i].x, &b[i].y, &cap[i]);

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        G[i][j] = getdis(a[i], b[j]);

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        scanf("%d", &plan[i][j]);

        for(int j = 1; j <= m; j++)
        {
            flow[j] = 0;
            for(int i = 1; i <= n; i++)
            flow[j] += plan[i][j];
        }

        build();
        if(spfa(t, t, n)) printf("OPTIMAL\n");
        else
        {
            printf("SUBOPTIMAL\n");
            for(int i = 1; i <= n; i++)
            {
            for(int j = 1; j <= m; j++)
            printf("%d ", plan[i][j]);
            puts("");
            }
        }
    }
    return 0;
}


POJ2175-消圈算法 题意: 题意:一个城市中有n个大楼,m个避难所,接下来给出n个大楼的坐标和人数,接下来m行给出每个避难所的坐标和容纳量,大楼到避难所的距离定义为曼哈顿距离+1,然后给出n行m列数据,第i行第j列代表从第i个大楼到第j个避难所去的人数,保证此方案合法,然后判断此方案是否所有人到避难所的距离和最小,是的话输出OPTIMAL,否则给出更好的方案,但不一定是最优的; 思路: 如果用最小费用最大流算法直接求... 阅读详情

相关推荐

消圈法解最小费用流(poj 2175 Evacuation Plan)

消圈算法: 先求最大流, 再在Gf中寻找负费用圈并沿它增广 改进: 增加附加弧(s, t), 费用大于s-t最大费用路(如VC), 弧上的初始流大于s-t最大流(如s出发的弧容量和加1)消圈算法将让尽量多的流移出附加弧, 因此得到的流是最大流. 最后附加弧可能有余量,应当忽略 实际效果: 最小费用增广路算法, 因为Gf中任何s-t路和t-s一定构成负费用圈 最坏情况分析: 每次找负费用圈

新博客地址:sensirly.github.io 3735

Poj2175(费用流,负环消圈)

挺好的题 充分利用了spfa 判断最费用流是否最优的充分必要条件是——图中是否存在负环  如果存在说明最费用流最优否则相反

ACM之梦 1835

POJ 2175 Evacuation Plan 最小费用流(找负圈)

点击打开链接 题意:n个居民点(x,y)人数为bi,m个避难点(x,y)容量为ci,两点之间花费为曼哈顿距离, 现在给出n个人到m个避难点的方案,问该方案花费是否最小,若不是则求出最优方案  居民点到避难点容量为min(bi,ci)花费为距离,源点s到居民点容量为bi,花费为0,避难点到汇点T容量为ci,花费为0. num=segma(b[i]),则求流量为num的最小费用流,

Masamiiiiii 688

消圈定理详解

消圈定理

favomj的博客 2434

从萤石云到GB28181:海康4G摄像机接入第三方平台的曲折之路

本文深入探讨了海康4G摄像机接入第三方平台时遇到的协议冲突问题,特别是GB28181与萤石云服务的互斥性。通过技术分析揭示了4G模块的定向流量限制和协议栈资源竞争等核心问题,并提供了有线接入测试和萤石云API二次开发等解决方案。最后总结了设备选型的关键参数和协议兼容性检查清单,为安防监控系统集成提供实用指导。

mango的博客 1027

FPGA调试利器:VIO与ILA在Vivado中的实战应用指南

本文详细介绍了Vivado中FPGA调试工具VIO与ILA的实战应用技巧。VIO作为虚拟输入/输出工具,可实时调整参数;ILA作为集成逻辑分析仪,能高效捕获内部信号。文章通过具体案例展示了如何配置ILA采样深度、触发条件,以及VIO的动态参数调整功能,帮助开发者提升上板调试效率,解决常见通信失败、数据不同步等问题。

weixin_42553471的博客 432

poj 2175

题意 n栋大楼,每个大楼里ni个人,m个避难所,每个避难所能容纳mi个人。每个建筑物都有坐标,每个人到达避难所的费用是距离+1。给了你一种安排,问这种安排是不是最优的。 题解 根据消圈定理 如果一个费用流的残留网络,他如果存在有一个负环 那么这个肯定不是最小费用流 因为你可以用这个负环去更新答案 反过来也是一样,如果没有负环,那么就是最小费用流 然后这题是一个模板题 我们考虑根据...

OZY的博客 643

实战指南:彻底清除TIA Portal卸载后遗留的SQL Server WinCC实例

本文详细介绍了如何彻底清除TIA Portal卸载后遗留的SQL Server WinCC实例,解决重新安装时的冲突问题。通过控制面板排查、注册表验证、安全卸载操作流程和注册表深度清理等步骤,确保完全移除残留组件,避免系统崩溃。文章还提供了安装环境重建策略和预防措施,帮助工程师高效管理西门子自动化软件。

weixin_30482181的博客 414

POJ 2175 Evacuation Plan 最小费用流 消负圈

解题思路:最开始一看标准的最小费用流的思路。 求出最小费用,然后和原先的答案进行比较。 不过这铁定会超时的,所以就死心吧... 接着从题目入手, Output的最后一句,Your plan need not be optimal itself, but must be valid and better than The City Council's one. 挺阴的... 题目

Veda_的专栏 842

RTX 50系显卡实战3DGS:从源码编译到避坑指南

本文详细介绍了在RTX 50系显卡上成功运行3D Gaussian Splatting(3DGS)的完整实战指南。针对新显卡架构与旧版预编译包不兼容的核心问题,文章提供了从源码编译CUDA扩展的清晰步骤,包括环境配置、依赖安装、核心模块diff-gaussian-rasterization和simple-knn的编译避坑方法,并涵盖了数据准备、模型训练与实时预览的全流程,帮助开发者高效利用新硬件进行3D重建。

weixin_28314575的博客 266

YOLOv8中的C2f模块代码详解

在YOLOv8网络结构中,C2f模块(CSP Bottleneck with 2 Convolutions)是一个关键组件,用于实现跨阶段部分聚合

zzzyyy8的博客 3万+

POJ2175 Evacuation Plan 【 最小费用流+消圈定理】

poj2175链接 消圈定理 所谓消圈定理,就是在某个流  中,如果其对应的残余网络没有负圈(剩余流量为  的边视为不存在),那它一定就是当前流量下的最小费用流。反之亦然。即「 是最小费用流等价于其残余网络中没有负圈」。 证明 怎么证明? 假设任意流 ,如果在相同流量下有比他更小费用的流 。观察流 ,在流  中,除源汇外所有点的流入量等于流出量,在流  中

tystrwor的博客 942

OpenClaw第2章:快速安装

本章将手把手教你安装 OpenClaw。

Maynor的博客 3249

poj 2175 Evacuation Plan(最小费用流 (消圈算法))

Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3689   Accepted: 980   Special Judge Description The City has a number of municipal buildin

youngkl博客 2151

嵌入式开发之瑞芯微RK356x--基本环境搭建

嵌入式环境搭建

NexyHO的博客 619

使用 Python 爬虫抓取 IMDb 电影信息 - 评分、评论与基本信息抓取教程

本文介绍了如何使用 Python 爬虫技术抓取 IMDb 电影评分、评论和基本信息。我们分别展示了通过IMDb API和两种方法抓取数据,并进行了数据存储、分析与可视化。在未来,我们还可以基于这些数据开发电影推荐系统、情感分析工具等,进一步丰富电影数据的应用场景。通过这些实践,您不仅学会了如何抓取和存储数据,还可以通过这些基础来构建自己的数据分析应用,获取更丰富的电影数据。

2201_76125261的博客 605

poj 2175 Evacuation Plan 最小费用流判定,消圈算法

题目链接 题意:一个城市有n座行政楼和m座避难所,现发生核战,要求将避难所中的人员全部安置到避难所中,每个人转移的费用为两座楼之间的曼哈顿距离+1,题目给了一种方案,问是否为最优方案,即是否全部的人员转移费用之和最小?若不是,输出SUBOPTIMAL,之后输出转移矩阵Ei,j.即第i座行政楼中Ei,j个人转移到第j座避难所;输出的不必是最优转移方案,只要比题解的sum小即可; 思路:直接建一个...

weixin_30817749的博客 135

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

POJ 2175 Evacuation Plan 消圈定理

题意:给出N个建筑物的坐标和里面的人数。给出M个防空洞

leodestiny 1122

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ 2175 Evacuation Plan 费用流消圈

题目:http://poj.org/problem?id=2175 题意:有n个建筑和m个防空洞,每个建筑里初始有一定数量的人,而防空洞有容量上限,现在让所有在建筑里的人转移到防空洞,从建筑跑到防空洞的花费为他们的曼哈顿距离,现在给出一种转移方案,问是不是最小花费,若不是,给出一种更小的花费(不需要是最小花费,比给定的小即可) 思路:第一次接触到费用流消圈。在本题中,用题目给定的方案构建出残余

弱智就要多努力 870

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

POJ - 2175 Evacuation Plan(最小费用最大流+消圈定理)

题目链接:点击查看 题目大意:给出n个建筑物和m个避难所,每个建筑物中的人需要到避难所中去避难,规定花费是建筑物和避难所的曼哈顿距离+1,现在给出一种路线方案,问这个方案是不是最优的,如果不是,输出比当前方案更优的答案 题目分析:虽然看完题目之后感觉是一道最小费用最大流的题目,但是如果直接照做的话会超时,可能是构图卡SPFA了,其实我们可以换个角度去思考,因为题目并不是要求我们输出最优解,而是...

Falcon的博客 590

解题报告 之 POJ2175 Evacuation Plan

POJ2175 Evacuation Plan 最小费用流 负权消去 The City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in case of a nuclear war. Each fallout shelter has a limited capacity in terms of a numbe

JUST CODE 868

Evacuation Plan-POJ2175最小费用消圈算法

Time Limit: 1000MS Memory Limit: 65536KSpecial JudgeDescriptionThe City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in cas

绝尘 1746

POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3256   Accepted: 855   Special Judge Description

fcbruce 3344

POJ2175:Evacuation Plan(消负圈)

Evacuation Plan Time Limit:1000MSMemory Limit:65536KTotal Submissions:5665Accepted:1481Special Judge 题目链接:http://poj.org/problem?id=2175 Description: The City has a number of municipal b...

dft539533的博客 230

【费用流消圈】POJ-2175 Evacuation Plan

Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K         Special Judge Description The City has a number of municipal buildings and a number of fallout shelter

JSure的代码库 1047
上一篇: hdu 3667 Transportation(最小费用流+拆边)
下一篇: hdu 4067 Random Maze(最小费用流)
KinFungYu
博客等级 码龄14年 39粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值