CSU 1506 Double Shortest Paths
2024-08-31 14:51:19
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 ;
}
最新文章
- Hibernate(开放源代码的对象关系映射框架)
- 火车采集器 帝国CMS7.2免登录发布模块
- 比较两个文件文件可以使用MD5比较工具
- python leetcode 日记--231. Power of Two
- [原]C# Winform 文件编码批量转换工具
- 【Hibernate步步为营】--双向关联一对一映射具体解释(一)
- 请一定记得升级java虚拟机
- 直播一:H.264编码基础知识详解
- 三种方式实现观察者模式 及 Spring中的事件编程模型
- Matlab-7:偏微分方程数值解法-李荣华-有限元解导数边界值的常微分(Galerkin方法)
- springboot application.properties 常用完整版配置信息
- 自定义函数hello,并注册到hive源码中并重新编译
- 国产 WEB UI 框架 (收费)-- Quick UI,Mini UI
- nvm 安装使用
- (网络编程)socketserver模块服务端实现并发
- Kubernetes之容器
- nodejs多语句查询
- eclipse中查找某一个字符串
- windows环境下搭建Java开发环境(三)——Maven环境配置使用 (转)
- HotSpot Generations
热门文章
- 使用xshell连接本地虚拟机中的Linux问题
- 关联对象 AssociatedObject 完全解析
- 路飞学城Python-Day8
- 微信小程序手势滑动卡片案例
- POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火
- 是否可以从一个static方法内部发出对非static方法的调用
- mysql5.7 安装方法 (跟旧的不一样了)
- 死锁的Dump文件
- [Angular + TsLint] Disable directive selector tslint error
- Implement Stack using Queues 用队列实现栈