POJ_3068_Shortest_pair_of_paths_(最小费用流)

时间:2022-08-31 14:06:32

描述


http://poj.org/problem?id=3068

危险品:N个仓库由M条有向边连接,每条边都有一定费用。将两种危险品从0运到N-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。求最小费用.

(好吧直接抄来的0.0)

"Shortest" pair of paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1220   Accepted: 493

Description

A chemical company has an unusual shortest path problem.

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

The
input will consist of multiple cases. The first line of each input will
contain N and M where N is the number of depots and M is the number of
individual shipping methods. You may assume that N is less than 64 and
that M is less than 10000. The next M lines will contain three values,
i, j, and v. Each line corresponds a single, unique shipping method. The
values i and j are the indices of two depots, and v is the cost of
getting from i to j. Note that these shipping methods are directed. If
something can be shipped from i to j with cost 10, that says nothing
about shipping from j to i. Also, there may be more than one way to ship
between any pair of depots, and that may be important here.

A line containing two zeroes signals the end of data and should not be processed.

Output

follow the output format of sample 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

分析


每个边的容量都是1,增加一个源点与汇点,与他们相连的边容量是2,费用是0.然后跑最小费用流即可.

注意:

1.加爆了什么的...好吧只有我才会犯这种智障错(撞墙).

ps.人生第一道最小费用流的题.还有两个月就NOI了,感觉这节奏不死真难.

 #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define read(a) a=getnum()
#define CC(i,a) memset(i,a,sizeof(i))
using namespace std; const int maxn=+,INF=<<;
int n,m;
bool vis[maxn];
int dis[maxn],prevv[maxn],preve[maxn];
struct edge
{
int to,cap,cost,rev;
edge(){}
edge(int a,int b,int c,int d):to(a),cap(b),cost(c),rev(d) {}
};
vector <edge> g[maxn]; inline int getnum()
{
int r=,k=; char c;
for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;
for(;c>=''&&c<='';c=getchar()) r=r*+c-'';
return k*r;
} void add_edge(int from,int to,int cap,int cost)
{
g[from].push_back(edge(to,cap,cost,g[to].size()));
g[to].push_back(edge(from,,-cost,g[from].size()-));
} void Spfa(int s)
{
for1(i,,n+) dis[i]=INF;
dis[s]=;
CC(vis,);
queue <int> q;
q.push(s);
vis[s]=true;
while(!q.empty())
{
int t=q.front(); q.pop();
vis[t]=false;
rep(i,g[t].size())
{
edge e=g[t][i];
if(e.cap>&&dis[e.to]-e.cost>dis[t])
{
dis[e.to]=dis[t]+e.cost;
prevv[e.to]=t;
preve[e.to]=i;
if(!vis[e.to])
{
vis[e.to]=true;
q.push(e.to);
}
}
}
}
} int min_cost_flow(int s,int t,int f)
{
int res=;
while(f>)
{
Spfa(s);
if(dis[t]==INF) return -;
int d=f;
for(int v=t;v!=s;v=prevv[v])
{
d=min(d,g[prevv[v]][preve[v]].cap);
}
f-=d;
res+=d*dis[t];
for(int v=t;v!=s;v=prevv[v])
{
edge &e=g[prevv[v]][preve[v]];
e.cap-=d;
g[v][e.rev].cap+=d;
}
}
return res;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("short.in","r",stdin);
freopen("short.out","w",stdout);
#endif
int cnt=;
while(scanf("%d%d",&n,&m)==&&(n!=||m!=))
{
for1(i,,m)
{
int from,to,cost;
read(from); read(to); read(cost);
from++; to++;
add_edge(from,to,,cost);
}
add_edge(,,,);
add_edge(n,n+,,);
printf("Instance #%d: ",++cnt);
int ans=min_cost_flow(,n+,);
if(ans==-)
{
printf("Not possible\n");
}
else
{
printf("%d\n",ans);
}
for1(i,,n+) g[i].clear();
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("short.out");
#endif
return ;
}

POJ_3068_Shortest_pair_of_paths_(最小费用流)的更多相关文章

  1. POJ2195 最小费用流

    题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...

  2. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

  3. POJ 2516:Minimum Cost(最小费用流)

    https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...

  4. POJ-2175 Evacuation Plan 最小费用流、负环判定

    题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...

  5. Going Home &lpar;hdu 1533 最小费用流&rpar;

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

  6. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  7. &lbrack;haoi2010&rsqb;订货 最小费用流

    这道题oj上的标签是动态规划,但我想不出来动态规划怎么搞,空间不爆,时间也要爆的: 好的,不扯淡,此题正常做法是最小费用流: 这道题我写了两遍,为什么呢?原因是第一次写的时候,不会写费用流,又恰好没带 ...

  8. FZU2143Board Game(最小费用流)

    题目大意是说有一个B矩阵,现在A是一个空矩阵(每个元素都为0),每次操作可以将A矩阵相邻的两个元素同时+1,但是有个要求就是A矩阵的每个元素都不可以超过K,求 这个的最小值 解题思路是这样的,新建起点 ...

  9. POJ 2516 Minimum Cost 最小费用流

    题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...

随机推荐

  1. Tesseract-OCR识别中文与训练字库实例

    关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试用一下,分享到博客让有同样兴趣的人少走弯路. 文中所用到的身份证图片资源是百度找的,如有侵权可联系我删除. 一. ...

  2. JAVA设计模式《二》

    上一篇为大家介绍了一下设计模式中的责任链模式,本篇为大家介绍一下关于设计模式中的单例模式与模板方法模式.单例模式的作用在于,保证应用中某个实例有且只有一个,单例模式又被分为:饱汉模式与饿汉模式,两者的 ...

  3. 软件工程个人作业-Week2

    第一部分  调研, 评测 必应词典客户端版本:安卓版5.2.2 bug描述一:在学习页面点击“单词挑战”或“我爱说英语”会弹出“加载失败,请稍后重试”,无论点击多少次都加载不出来. bug描述二:在未 ...

  4. FreeSWITCH的TLS加密

    听着很高大上(实际也很实用)的加密机制,在FreeSWITCH里配置支持竟然这么简单! Greate FreeSWITCH and Greate Programmer! ① cd /usr/local ...

  5. ZOJ 1125 Floating Point Numbers

    原题链接 题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示. 解法:注释比较清楚了,注意浮点运算的四舍五入问题. 参考代码: #include<iostream> ...

  6. SmartAdmin 打开速度慢的原因

    最近在使用SmartAdmin做个小东西,发布在公网上,我的机器打开飞快,但是到了其它人的机器上变得极慢了.而且在我的手机上也打开变慢.     查找原因,原来如此.      <link re ...

  7. Javascript 中判断是谷歌浏览器和IE浏览器的方法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 全文检索luncence

    检索技术基本原理: 最主要的两点是  1.如何创建索引 2.如何查询.  分析需求: 好几篇文档,从这些文档找关键词,一种方式是顺序一个个遍历,加入这些文档量很多,就花费太长时间了,第二种是建立索引, ...

  9. TagBuilder 性能如此低下&quest;

    本文来自:http://www.cnblogs.com/zhuisha/archive/2010/03/12/1684022.html 需要通过ASP.NET MVC生成一个列表,MVC里面根正苗红的 ...

  10. 如何解决更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外 提示:一个版本库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更 提示:(如 &&num;39&semi;git pull &period;&period;&period;&&num;39&semi;)。

    不要通过网页提交,通过网页提交一次,然后在终端再次push的时候,会认为网上代码仓库已经被其他地方提交过一次代码,此时会拒绝终端push 这个时候只能是pull,然后才能再次在终端提交. 也就是说,避 ...