Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2464    Accepted Submission(s): 1052

Problem Description
There
are N cities, and M directed roads connecting them. Now you want to
transport K units of goods from city 1 to city N. There are many robbers
on the road, so you must be very careful. The more goods you carry, the
more dangerous it is. To be more specific, for each road i, there is a
coefficient ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.
 
Input
There
are several test cases. The first line of each case contains three
integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0
<= K <= 100). Then M lines followed, each contains four integers
(ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5)
 
Output
Output
one line for each test case, indicating the minimum cost. If it is
impossible to transport all the K units of goods, output -1.

 
Sample Input
2 1 2
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
 
Sample Output
4
-1
3
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3661 3664 3665 3666 3669
本题的题一很简单,就是一个人需要运送k单位的货物,但是沿途的道路,过每个道路都需要花钱,cost的系数为a,每运送x单位的货物需要华为a*x*x的价格,我们可以在建立边的时候将其拆开
比如5单位的货物,话费需要a*5*5,但是我们可以将它一个一个单位的分开,就是1,4-1,9-4,16-9等等,每个边的容量都为1,除此之外在建立一个超级原点和一个超级汇点,直接跑最小费用最大流的模板就可以了
下面附上代码,由于第一次边的大小没有掌握好,结果超了内纯一次,
下次可得小心了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
//最小费用最大流,求最大费用只需要取相反数,结果取相反数即可。
//点的总数为 N,点的编号 0~N-1
const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
} edge[MAXM*];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
N = n;
tol = ;
memset(head,-,sizeof (head));
}
void addedge (int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = ;
edge[tol].cost = -cost;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = ; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i]. next)
{
int v = edge[i]. to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i]. cost )
{
dis[v] = dis[u] + edge[i]. cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
if(Min > edge[i].cap - edge[i]. flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i]. cost * Min;
}
flow += Min;
}
return flow;
}
int main(){
int n,m,sta;
while(scanf("%d%d%d",&n,&m,&sta)!=EOF){
memset(pre,,sizeof(pre));
memset(dis,,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(edge,,sizeof(edge));
init(n+);
int u,v,a,c;
for(int i=;i<m;i++){
scanf("%d%d%d%d",&u,&v,&a,&c);
for(int j=;j<c;j++)
addedge(u,v,,a*(*j+)); }
int ans1=;
addedge(,,sta,);
addedge(n,n+,sta,);
int temp=minCostMaxflow(,n+,ans1);
if(temp!=sta)
puts("-1");
else
printf("%d\n",ans1); }
}
 
14745869 2015-09-07 20:51:51 Accepted 3667 998MS 3148K 2986 B G++

      

hdu3667的更多相关文章

  1. hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流

    /** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...

  2. HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)

    题目链接:https://vjudge.net/problem/HDU-3667 Transportation Time Limit: 2000/1000 MS (Java/Others)    Me ...

随机推荐

  1. 洛谷 P1074 靶形数独

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...

  2. TortoiseGit和git bash冲突解决

    软件版本 TortoiseGit版本: TortoiseGit 2.5.0.0 (C:\Program Files\TortoiseGit\bin) git version 2.8.1.windows ...

  3. Mongodb之failed to create service entry worker thread

    Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...

  4. UVA 1471 Defense Lines 防线 (LIS变形)

    给一个长度为n的序列,要求删除一个连续子序列,使剩下的序列有一个长度最大的连续递增子序列. 最简单的想法是枚举起点j和终点i,然后数一数,分别向前或向后能延伸的最长长度,记为g(i)和f(i).可以先 ...

  5. 团队作业-Beta冲刺(周三)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...

  6. db2的定时备份

    定时任务: db2.bat db2cmd -i -w db2_backup.bat exit db2_backup.bat db2 connect to TEST db2 force applicat ...

  7. MarkdownPad 2 Pro 注册码

    MarkdownPad 2 Pro 注册码 MarkdownPad 是 Windows 平台上一个功能完善的 Markdown 编辑器. 提供了语法高亮和方便的快捷键功能,给您最好的 Markdown ...

  8. React动态import()

    React动态import() react-router@v4代码分离,推荐的import().这里分享webpack配置和使用方法. 首先安装两个必须的包 cnpm i react-loadable ...

  9. vue 前端判断输入框不能输入0 空格。特殊符号。

    oninput="value=value.replace(/[^\d.]/g,'').replace(/\.{2,}/g,'.').replace('.','$#$').replace(/\ ...

  10. 看了下opengl相关的资料,踩了一个坑,记录一下

    2019/03/10 下午看了下关于opengl的资料,然后把敲了下代码,然后程序报错了.代码如下: #include <glad/glad.h> #include <GLFW/gl ...