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

题目大意:

从0~n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径。

————————————————————————————

POJ2135魔改版。

按照那题的思路并且把点拆成中间连一条容量为1的边即可。

切了切了。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long ll;
const int INF=1e9;
const int N=;
const int M=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int nxt;
int to;
int w;
int b;
}edge[M];
int head[N],cnt=-;
void add(int u,int v,int w,int b){
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].b=b;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
int dis[N];
bool vis[N];
inline bool spfa(int s,int t,int n){
deque<int>q;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)dis[i]=INF;
dis[t]=;q.push_back(t);vis[t]=;
while(!q.empty()){
int u=q.front();
q.pop_front();vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
int b=edge[i].b;
if(edge[i^].w&&dis[v]>dis[u]-b){
dis[v]=dis[u]-b;
if(!vis[v]){
vis[v]=;
if(!q.empty()&&dis[v]<dis[q.front()]){
q.push_front(v);
}else{
q.push_back(v);
}
}
}
}
}
return dis[s]<INF;
}
int ans=;
int dfs(int u,int flow,int m){
if(u==m){
vis[m]=;
return flow;
}
int res=,delta;
vis[u]=;
for(int e=head[u];e!=-;e=edge[e].nxt){
int v=edge[e].to;
int b=edge[e].b;
if(!vis[v]&&edge[e].w&&dis[u]-b==dis[v]){
delta=dfs(v,min(edge[e].w,flow-res),m);
if(delta){
edge[e].w-=delta;
edge[e^].w+=delta;
res+=delta;
ans+=delta*b;
if(res==flow)break;
}
}
}
return res;
}
inline int costflow(int S,int T,int n){
int flow=;
while(spfa(S,T,n)){
do{
memset(vis,,sizeof(vis));
flow+=dfs(S,INF,T);
}while(vis[T]);
}
return flow;
}
void restart(){
memset(head,-,sizeof(head));
cnt=-;
ans=;
return;
}
int main(){
int ecnt=,n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n&&m){
restart();
ecnt++;
int F=,E=n;
int S=n*+,T=S+;
add(S,F,,);add(F,S,,);
add(E,T,,);add(T,E,,);
for(int i=;i<=n-;i++){
add(i,i+n,,);
add(i+n,i,,);
}
for(int i=;i<=m;i++){
int u=read()+;
int v=read()+;
int b=read();
if(u!=&&u!=n)u+=n;
add(u,v,,b);
add(v,u,,-b);
}
int flow=costflow(S,T,T);
printf("Instance #%d: ",ecnt);
if(flow!=){
printf("Not possible\n");
}else printf("%d\n",ans);
}
return ;
}

POJ3068:"Shortest" pair of paths——题解的更多相关文章

  1. POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...

  2. "Shortest" pair of paths[题解]

    "Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...

  3. POJ3068 "Shortest" pair of paths

    嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...

  4. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  5. poj 3068 "Shortest" pair of paths

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1407 ...

  6. UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解

    题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...

  7. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  8. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

  9. UVALIVE 2927 "Shortest" pair of paths

    裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...

随机推荐

  1. leetcode--笔记8 Fizz Buzz

    题目要求: Write a program that outputs the string representation of numbers from 1 to n. But for multipl ...

  2. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  3. 「日常训练」 Counting Cliques(HDU-5952)

    题意与分析 题源:2016ACM/ICPC沈阳现场赛. 这题让我知道了什么是团,不过最恶心的还是这题的数据了,卡了无数次- - 解决方法是维护一个G数组,不能去遍历邻接矩阵.至少我改了这么一个地方就过 ...

  4. 为什么测试人员必须掌握Linux?

    相信点进来的小伙伴不是对Linux感兴趣就是对测试感兴趣了,也希望本文可以帮助之前接触过Linux的小伙伴找到继续坚持学习下去的动力,之前没接触过Linux的小伙伴也能找到开始学习Linux的兴趣. ...

  5. TW实习日记:第22天

    今天开发项目的还没完成的功能点,没什么难的,样式复制粘贴,JSON表单配一配,接口调一调,基本就完成了.不过中间在写后台的一些接口时,发现被自己之前写的一些方法给坑了.为什么这样说呢,因为在之前的几个 ...

  6. 204. Singleton

    Description Singleton is a most widely used design pattern. If a class has and only has one instance ...

  7. 【token接口】-jmeter

    token 接口 3步骤 1.登录接口 2.提取登录接口的token 3.http 信息管理头   把提取的cookie传入  就可以了

  8. java并发总览

  9. Linux内核设计笔记7——中断

    中断与中断处理 何为中断? 一种由设备发向处理器的电信号 中断不与处理器时钟同步,随时可以发生,内核随时可能因为中断到来而被打断. 每一个中断都有唯一一个数字标志,称之为中断线(IRQ) 异常是由软件 ...

  10. 将System.Drawing.Bitmap转换为Direct2D.D2DBitmap

    最近在尝试Direct2D编程,挺好玩的. 但是有时候还是会用到GDI+来生成图片,但D2D绘图需要用到自己的D2DBitmap类. 因此需要转换,查阅了下网上的资料,写了这么一个方法: using ...