POJ 3068 运送危险化学品 最小费用流 模板题
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1215 | Accepted: 491 |
Description
There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That's easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling --- available only at the first and last depots. To begin, they need to know if it's possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.
Your program must simply determine the minimum cost or, if it's not possible, conclusively state that the shipment cannot be made.
Input
A line containing two zeroes signals the end of data and should not be processed.
Output
Sample Input
2 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0
Sample Output
Instance #1: Not possible
Instance #2: 40
Instance #3: 73
Source
m条有向边连接了n个仓库,每条边都有一定费用。
将两种危险品从0运到n-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。
求最小的费用是多少。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
const int N = 70;
const int M=10000+100;
struct edge{
int to,cap,cost,rev;
};
vector<edge> G[1005];
int dist[1005],inq[1005],prev[1005],prel[1005];
int n,m,x,y,c;
void add_edge(int u,int v,int cost)
{
G[u].push_back(edge{v,1,cost,G[v].size()});
G[v].push_back(edge{u,0,-cost,G[u].size()-1});
}
int mincost(int s,int t,int f)
{
int ans=0;
while(f>0)
{
memset(dist,inf,sizeof(dist));
memset(inq,0,sizeof(inq));
dist[s]=0;
queue<int> q;
q.push(s);
inq[s]=1;
MM(prev,-1);
while(!q.empty())
{
int u=q.front();
q.pop();inq[u]=0;
for(int j=0;j<G[u].size();j++)
{
edge &e=G[u][j];
if(e.cap>0&&dist[e.to]>dist[u]+e.cost)
{
dist[e.to]=dist[u]+e.cost;
prev[e.to]=u;
prel[e.to]=j;
if(!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
}
}
}
}
for(int i=t;i>s;)
{
int f=prev[i];
if(f==-1) return -1;//不存在符合要求的路径则退出
int j=prel[i];
G[f][j].cap-=1;
G[i][G[f][j].rev].cap+=1;
ans+=G[f][j].cost;
i=prev[i];
}
f-=1;//因为每条边容量都为1
}
return ans;
} int main()
{
int kk=0;
while(~scanf("%d %d",&n,&m)&&(n||m))
{
for(int i=0;i<n;i++) G[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,c);
}
int ans=mincost(0,n-1,2);
if(ans==-1) printf("Instance #%d: Not possible\n",++kk);
else printf("Instance #%d: %d\n",++kk,ans);
}
return 0;
}
分析:最小费用流模板题,直接套的模板,刚开始忘记清空数组被TLE了
POJ 3068 运送危险化学品 最小费用流 模板题的更多相关文章
- POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
- POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...
- POJ 3264 Balanced Lineup(模板题)【RMQ】
<题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...
- POJ 1330 Nearest Common Ancestors (模板题)【LCA】
<题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ:3461-Oulipo(KMP模板题)
原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- POJ 1269 - Intersecting Lines - [平面几何模板题]
题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know ...
随机推荐
- 概率DP(放棋子)Domination
题意:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827369874 给你n*m的棋盘,我们定义放满棋盘是:任意一行一列至少有一 ...
- 自动化测试_百度--糯米中--携程-出行<一>
1:接下来我们看看思路 和相应的功能 使用python+selenium+unittest完成测试脚本 打开chrome浏览器,窗口最大化,设置等待时间10s 打开百度首页 鼠标移动到更多产品,点击 ...
- shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机
1.NGINX启动脚本 #!/bin/bash # chkconfig: 235 32 62 # description: nginx [ -f /etc/init.d/functions ] &am ...
- easyui在table单元格中添加进度条
function XR_jd(alue, rowData, rowIndex){ var value; ...... var htmlstr = '<div class="easyui ...
- EJS学习(二)之语法规则上
标签含义 <% %> :'脚本' 标签,用于流程控制,无输出即直接使用JavaScript语言. <%= %>:转义输出数据到模板(输出是转义 HTML 标签)即在后端定义的变 ...
- 11 Python之初识函数
---恢复内容开始--- 1. 什么是函数? f(x) = x + 1 y = x + 1 函数是对功能或者动作的封装 2. 函数的语法和定义 def 函数名(): 函数体 调用: 函数名() 3. ...
- Vue与Angular以及React的三者之间的区别
1.与AngularJS的区别 相同点:都支持指令:内置指令和自定义指令:都支持过滤器:内置过滤器和自定义过滤器:都支持双向数据绑定:都不支持低端浏览器. 不同点:AngularJS的学习成本高,比如 ...
- javaScript中 数组的新方法(reduce)
定义和用法 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: redu ...
- chrome 浏览器安装 postman
chrome 浏览器安装 postman(插件下载见文章末尾) 1.安装方法 将下载的crx插件拖拽到chrome浏览器即可安装成功. 2.特殊情况 问题: chrome73版本后拖拽安装chrome ...
- [转] JAVA分为三个体系,JavaSE,JavaEE,JavaME(J2ME)的区别以及各个版
Java SE(JavaPlatform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程 ...