裸的费用流。一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的。Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling

一个点只能用一次??

忽略这句话就直接费用流 此题类似dijkstra,dijkstra那道

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 140
const int INF = 0x3f3f3f3f ;
int N,M;
queue<int>q;
struct node
{
int u,v,next;
int flow,cap,cost;
}edge[MAXN * MAXN * ];
int cnt,src,tag;
int C,F;
bool inq[MAXN];int d[MAXN];
int head[MAXN],p[MAXN];
void add(int u,int v,int cap,int cost)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = cap;
edge[cnt].flow = ;
edge[cnt].cost = cost;
edge[cnt].next = head[u];
head[u] = cnt++;
//反向
edge[cnt].v = u;
edge[cnt].u = v;
edge[cnt].flow = ;
edge[cnt].cap = ;
edge[cnt].cost = - cost;
edge[cnt].next = head[v];
head[v] = cnt++;
}
void read()
{
cnt = ;
memset(head,-,sizeof(head));
src = ; tag = N + ;
add(src,,,);
//for (int i = 1; i <= N; i++) add(i,i + N,1,0);
for (int i = ; i <= M; i++)
{
int u ,v ,w;
scanf("%d%d%d",&u,&v,&w);
u++;v++;
//u的后向点 链接 v 的前向点
add(u,v,,w);
}
add(N,tag,,);
}
bool SPFA(int s, int t)
{
while (!q.empty()) q.pop();
memset(inq,false,sizeof(inq));
memset(d,0x3f,sizeof(d));
memset(p,-,sizeof(p));
d[s] = ;
q.push(s);
inq[s] = true;
while (!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] > d[u] + edge[i].cost && edge[i].cap > edge[i].flow)
{
d[v] = d[u] + edge[i].cost;
p[v] = i;
if (!inq[v])
{
q.push(v);
inq[v] = true;
}
}
}
}
return d[tag] != INF;
}
void slove()
{
C = F = ;
while(SPFA(src,tag))
{
int a = INF;
for (int i = p[tag]; i != -; i = p[edge[i].u])
a = min(a,edge[i].cap - edge[i].flow);
for (int i = p[tag]; i != -; i = p[edge[i].u])
{
edge[i].flow += a;
edge[i ^ ].flow -= a;
}
C += d[tag] * a;
F += a;
}
}
int main()
{
//freopen("sample.txt","r",stdin);
int kase = ;
while (scanf("%d%d",&N,&M) != EOF)
{
if (N == && M == ) break;
read();
slove();
if (F < ) printf("Instance #%d: Not possible\n",kase++);
else printf("Instance #%d: %d\n",kase++,C);
}
return ;
}

UVALIVE 2927 "Shortest" pair of paths的更多相关文章

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

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

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

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

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

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

  4. poj 3068 "Shortest" pair of paths

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

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

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

  6. POJ3068 "Shortest" pair of paths

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

  7. POJ3068:"Shortest" pair of paths——题解

    http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...

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

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

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

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

随机推荐

  1. [Codeforces947D]Riverside Curio(思维)

    Description 题目链接 Solution 设S[i]表示到第i天总共S[i]几个标记, 那么满足S[i]=m[i]+d[i]+1 m[i]表示水位上的标记数,d[i]表示水位下的标记数 那么 ...

  2. ABAP CDS - SELECT, association

    ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...

  3. DNS域名解析服务(bind)

    DNS(Domain Name System,域名系统): 用于管理和解析域名与IP地址对应关系的技术. 简单来说,就是能够接受用户输入的域名或IP地址,然后自动查找与之匹配(或者说具有映射关系)的I ...

  4. 18 Django-组件拾遗

    一 Django的form组件 forms组件 二 Django的model form组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的 ...

  5. 3. 与服务器对话:理解 HTTP 协议

    0.服务器与本地交换机制 2.详解HTtp服务 (1)与服务器对话的流程 (2)Reque 请求 (3)Response 响应 200 成功 404 没有网页 (4)Get/Post区别 get查询数 ...

  6. 剑指Offer - 九度1214 - 丑数

    剑指Offer - 九度1214 - 丑数2013-11-21 21:06 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. ...

  7. 【非原创】tomcat 安装时出现 Failed to install Tomcat7 service

    tomcat 安装时出现 Failed to install Tomcat7 service 今天在安装tomcat时提示 Failed to install Tomcat7 service了,花了大 ...

  8. [译]16-spring基于注解的配置元数据

    从spring2.5起spring框架开始支持java注解的配置元数据.所以除了使用xml配置文件来描述bean的装配之外,你还 可以使用基于java注解的配置元数据来完成同样的功能. spring框 ...

  9. Linq语法和C#6.0

    一. linq 1.简介: 能用linq实现的基本都可以用扩展方法实现: 举例: 查询ID>1的狗有如下两种写法 (1)var  r1=dogs.where(d=>d.id>1) ( ...

  10. mysql Access denied for user 'root'@'localhost'问题解决

    问题描述: 系统安装mysql的过程中,没有提示配置用户名和密码相关的信息,安装完毕后,登录报错. 表现现象为: mysql -u root -p [输入root密码] 界面提示: ERROR 169 ...