1506: Double Shortest Paths

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 49  Solved: 5

Description

Input

There will be at most 200 test cases. Each case begins with two integers n, m (1<=n<=500, 1<=m<=2000), the number of caves and passages. Each of the following m lines contains four integers u, v, di and ai (1<=u,v<=n, 1<=di<=1000, 0<=ai<=1000). Note that there can be multiple passages connecting the same pair of caves, and even passages connecting a cave and itself.

Output

For each test case, print the case number and the minimal total difficulty.

Sample Input

4 4
1 2 5 1
2 4 6 0
1 3 4 0
3 4 9 1
4 4
1 2 5 10
2 4 6 10
1 3 4 10
3 4 9 10

Sample Output

Case 1: 23
Case 2: 24

HINT

 

Source

湖南省第十届大学生计算机程序设计竞赛

解题:费用流

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -){
to = x;
flow = y;
cost = z;
next = nxt;
}
};
arc e[maxn*maxn];
int head[maxn],d[maxn],p[maxn];
int tot,S,T;
void add(int u,int v,int flow,int cost){
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool in[maxn];
bool spfa(){
queue<int>q;
for(int i = S; i <= T; ++i){
p[i] = -;
in[i] = false;
d[i] = INF;
}
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]){
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
return p[T] > -;
}
int solve(){
int ans = ;
while(spfa()){
int minF = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
minF = min(minF,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]){
e[i].flow -= minF;
e[i^].flow += minF;
}
ans += d[T]*minF;
}
return ans;
}
int main(){
int n,m,u,v,ai,di,cs = ;
while(~scanf("%d %d",&n,&m)){
memset(head,-,sizeof(head));
S = tot = ;
T = n + ;
for(int i = ; i < m; ++i){
scanf("%d %d %d %d",&u,&v,&ai,&di);
add(u,v,,ai);
add(u,v,,ai+di);
}
add(S,,,);
add(n,T,,);
printf("Case %d: %d\n",cs++,solve());
}
return ;
}

CSU 1506 Double Shortest Paths的更多相关文章

  1. UVA 12821 Double Shortest Paths

    Double Shortest PathsAlice and Bob are walking in an ancient maze with a lot of caves and one-way pa ...

  2. CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)

    题意:2个人从1走到n,假设一条路第一次走则是价值di,假设第二次还走这条路则须要价值di+ai,要你输出2个人到达终点的最小价值! 太水了!一条边建2次就OK了.第一次价值为di,第二次为ai+di ...

  3. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  4. CSU 1506(最小费用最大流)

    传送门:Double Shortest Paths 题意:有两个人:给出路径之间第一个人走所需要的费用和第二个人走所需要的费用(在第一个人所需的 费用上再加上第二次的费用):求两个人一共所需要的最小费 ...

  5. Shortest Paths

    最短路径 APIs 带权有向图中的最短路径,这节讨论从源点(s)到图中其它点的最短路径(single source). Weighted Directed Edge API 需要新的数据类型来表示带权 ...

  6. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

  7. Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...

  8. 【例题收藏】◇例题·II◇ Berland and the Shortest Paths

    ◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...

  9. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

随机推荐

  1. java中三个类别加载器的关系以及各自加载的类的范围

    Java在需要使用类别的时候,才会将类别加载,Java的类别载入是由类别载入器(Class loader)来达到的,预设上,在程序启动之后,主要会有三个类别加载器:Bootstrap Loader.E ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)

    Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...

  3. Django——Model

    一. ORM 在 MVC 或者说 MTV 设计模式中,模型(M)代表对数据库的操作.那么如何操作数据库呢? 我们可以在 Python 代码中嵌入 SQL 语句. 但是问题又来了,Python 怎么连接 ...

  4. 安卓、safari和微信各个浏览器的设计标准

  5. 洛谷P2617 Dynamic Rankings 主席树 单点修改 区间查询第 K 大

    我们将线段树套在树状数组上,查询前预处理出所有要一起移动的节点编号,并在查询过程中一起将这些节点移到左右子树上. Code: #include<cstdio> #include<cs ...

  6. Java默认方法

    示例1 interface InterfaceA { default void say() { System.out.println("InterfaceA"); } } publ ...

  7. GenIcam标准(二)

    2     GenApi模块 – 配置相机 2.1. 简介 GenApi模块解决如何去配置相机的问题.主要的思路是,让相机生产厂商为他们的相机提供机器可以识别的产品说明.这些相机描述文件(camera ...

  8. C语言中数据类型的字节数

    类型 16位 32 位 64位 char 1 1 1 short int 2 2 2 int 2 4 4 unsigned int 2 4 4 float 4 4 4 double 8 8 8 lon ...

  9. [Javascript] Improve Composition with the Compose Combinator

    To make our composition more readable and easier to name we are going to ceate a compose function we ...

  10. GridView中使用CheckBox

    asp.net中checkbox是向server提交时才干触发事件 把该控件的autopostback设置为true.才干响应事件  protected void Checkbox_CheckedCh ...