HDU 3667.Transportation 最小费用流
Transportation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3045 Accepted Submission(s): 1318
You should find out the minimum cost to transport all the goods safely.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=1e3+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e13+;
struct edge
{
int from,to;
ll c,w;
};
int n;
vector<edge>es;
vector<int>G[maxn];
ll dist[maxn];
int pre[maxn];
inline void addedge(int u,int v,ll c,ll w)
{
es.push_back((edge)
{
u,v,c,w
});
es.push_back((edge)
{
v,u,,-w
});
int x=es.size();
G[u].push_back(x-);
G[v].push_back(x-);
} bool spfa(int s,int t)
{
static std::queue<int> q;
static bool inq[maxn];
for(int i=; i<=n+; i++) dist[i]=INF,inq[i]=false;
pre[s]=-;
dist[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=false;
for(int i=; i<G[u].size(); i++)
{
edge e=es[G[u][i]];
if(e.c&&dist[e.to]>dist[u]+e.w)
{
pre[e.to]=G[u][i];
dist[e.to]=dist[u]+e.w;
if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
}
}
}
return dist[t]<inf;
} void dinic(int s,int t,ll f)
{
ll flow=,cost=;
while(spfa(s,t))
{
ll d=f;
for(int i=t; i!=s; i=es[pre[i]].from)
d=min(d,es[pre[i]].c);
f-=d;
flow+=d;
cost+=d*dist[t];
for(int i=t; i!=s; i=es[pre[i]].from)
{
es[pre[i]].c-=d;
es[pre[i]^].c+=d;
}
if(f<=) break;
}
if(f) puts("-1");
else printf("%lld\n",cost);
} int main()
{
int m;
ll k;
while(~scanf("%d%d%lld",&n,&m,&k))
{
for(int i=; i<=m; i++)
{
int u,v;
ll c,w;
scanf("%d%d%lld%lld",&u,&v,&w,&c);
for(ll t=; t<=c; t++)
addedge(u,v,1LL,(t*t-(t-)*(t-))*w);
}
dinic(,n,k);
es.clear();
for(int i=; i<=n+; i++) G[i].clear();
}
return ;
}
最小费用流
HDU 3667.Transportation 最小费用流的更多相关文章
- HDU 3667 Transportation(网络流之费用流)
题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...
- hdu 3667 拆边加最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 3667 费用流 拆边 Transportation
题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- HDU 3667
http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...
- hdu 3667(最小费用最大流+拆边)
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)
题目链接:https://vjudge.net/problem/HDU-3667 Transportation Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDU 3667 费用流(拆边)
题意:有n个城市(1~n),m条有向边:有k件货物要从1运到n,每条边最多能运c件货物,每条边有一个危险系数ai,经过这条路的费用需要ai*x2(x为货物的数量),问所有货物安全到达的费用. 思路:c ...
- HDU Destroy Transportation system(有上下界的可行流)
前几天正看着网络流,也正研究着一个有上下界的网络流的问题,查看了很多博客,觉得下面这篇概括的还是相当精确的: http://blog.csdn.net/leolin_/article/details/ ...
随机推荐
- vscode 调试 TypeScript
安装 typescript 依赖 npm install typescript --save-dev 目录结构: 添加 tsconfig.json 主要是将 sourceMap 设置为true. { ...
- Java备份文件
文件名后面补时间: public static void initFile(String sPath) { SimpleDateFormat df = new SimpleDateFormat(&qu ...
- linux服务samba与ftp篇
samba复习: 1.下载samba:yum -y install samba 2.打开配置文件/etc/samba/smb.conf输入: [共享文件名] path = 目录名 (事先创建) pub ...
- django之 使用views.py里面的函数对表进行增删改查 内容(models.py中表的创建、views.py中函数的使用,基于对象的跨表查询)
models之对于表的创建有以下几种: 一对一:ForeignKey("Author",unique=True), OneToOneField("Author" ...
- kubernetes之configmap,深度解析mountPath,subPath,key,path的关系和作用
参考:https://www.cnblogs.com/breezey/p/6582082.html 我们知道,在几乎所有的应用开发中,都会涉及到配置文件的变更,比如说在web的程序中,需要连接数据库, ...
- Citrix XenApp工作原理
Citrix XenApp™作为一种Windows®应用交付系统,可在数据中心集中管理应用,并将应用按需交付给身处各地.使用各种设备的用户.利用集成的应用虚拟化技术,XenApp克服了传统应用部署方法 ...
- C# 方法参数 out、ref、param 详解
ref和out都对函数参数采用引用传递形式——不管是值类型参数还是引用类型参数,并且定义函数和调用函数时都必须显示生命该参数为 ref/out形式.两者都可以使函数传回多个结果. ref 类似于 PH ...
- 如何在js中使用递归
很久没写博客了... 内容后补
- [C语言]进阶|图形库
---------------------------------------------------------------------- // main.c // Created by weich ...
- css选择器querySelectorAll
* querySelectorAll(css的选择器)* 通过css的选择器获取到的一组元素* 获取的也是类数组** 主语* document 从整个页面去获取一组元素* 父级 从父级下去获取一组元素 ...