hdu3667

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

2010 Asia Regional Harbin

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 = 1005;
const int MAXM = 20005;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow,cost;
} edge[MAXM*4];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
    N = n;
    tol = 0;
    memset(head,-1,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 = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}
bool spfa(int s,int t)
{
    queue<int>q;
    for(int i = 0; i < N; i++)
    {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i !=  -1; 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] == -1)return false;
    else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
    int flow = 0;
    cost = 0;
    while(spfa(s,t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            if(Min > edge[i].cap - edge[i]. flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].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,0,sizeof(pre));
       memset(dis,0,sizeof(dis));
       memset(vis,false,sizeof(vis));
       memset(edge,0,sizeof(edge));
      init(n+2);
      int u,v,a,c;
      for(int i=0;i<m;i++){
          scanf("%d%d%d%d",&u,&v,&a,&c);
          for(int j=0;j<c;j++)
          addedge(u,v,1,a*(2*j+1));

      }
      int ans1=0;
      addedge(0,1,sta,0);
      addedge(n,n+1,sta,0);
      int temp=minCostMaxflow(0,n+1,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++
      

时间: 2024-10-29 19:10:36

hdu3667的相关文章

hdu3667 Transportation 拆边法+最小费用最大流

/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由m条有向边连接.要从城市1运输k流量到城市n.每条边有可以运输的流量容量,以及费用系数ai. 费用系数指该条边的费用为该条边的运输流量x的平方乘以ai.即ai*x^2. 如果无法运输k流量,输出-1,否则输出从城市1运输k流量到城市n的最小花费. 思路:拆边法+最小费用最大流 假设从u->v 容量为

费用流 hdu3667 Transportation

传送门:点击打开链接 题意:n个节点m条有向边,每条有向边的容量是C,且费用是a*x^2,x是流量,问从1运送k流量到n的最小费用 一般做的费用流边的费用都是固定的,而这题并不是固定的. 但是,看到了C<=5,其实就是在提示可以拆边.. 假如C是3,我们就可以把一条边拆成3条边. 假如不拆,如果通过的流量是1,2,3,那么费用分别是a,4a,9a 如果拆成3条边,那么3条边的费用分别是a,3a,5a,容量都是1 这样就完美的解决了边的费用问题了~ #include<map> #inclu