uva 1486,费用流

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$ \le$N$ \le$100, 1$ \le$M$ \le$5000, 0$ \le$K$ \le$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$ \le$ui, vi$ \le$N, 0 < ai$ \le$100, Ci$ \le$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

书上讲过的处理方法,当时没看懂,做题时YY了出来。
因为cap<=5,所以有这几种:1, 4, 9, 16, 25
其实它们可分别表示为和的方式:(1), (1+3), (1+3+5),(1+3+5+7), (1+3+5+7+9)
那么对以后的项是否也可这么做呢?
因为(i从1到n求和)(2*i-1) = (2*n-1 + 1) * n/2 = n^2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define cint const int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define INF 100000000
#define MAXN 110
#define MAXM 50010

struct edge{
    int u, v, cap, flow, cst, nxt;
}e[MAXM];
int h[MAXN], cc;

void add(int u, int v, int cap, int cst){
    e[cc]=(edge){u, v, cap, 0, cst, h[u]};
    h[u]=cc++;
    e[cc]=(edge){v, u, 0, 0, -cst, h[v]};
    h[v]=cc++;
}

int d[MAXN], p[MAXN], inq[MAXN];
int a[MAXN], k;
void mcmf(cint s, cint t, cint n, int &mincst, int &maxflow){
    mincst=maxflow=0;
    while(1){
        queue<int> q;   q.push(s);
        memset(inq, 0, sizeof(inq));
        for(int i=0; i<n; i++) d[i]=INF;
        inq[s]=1;   d[s]=0;
        a[s]=INF;
        while(!q.empty()){
            int u=q.front();    q.pop();    inq[u]=0;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v=e[i].v, cap=e[i].cap, ef=e[i].flow, cst=e[i].cst;
                if(d[v]>d[u]+cst && cap>ef){
                    d[v]=d[u]+cst;
                    a[v]=MIN(a[u], cap-ef);
                    p[v]=i;
                    if(!inq[v]) q.push(v), inq[v]=1;
                }
            }
        }
        if(d[t]==INF) break;
        maxflow+=a[t];
        mincst+=a[t]*d[t];
        for(int u=t; u!=s; u=e[p[u]].u){
            e[p[u]].flow+=a[t];
            e[p[u]^1].flow-=a[t];
        }
    }
}

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int n, m;
    while(scanf(" %d %d %d", &n, &m, &k)==3){
        int i, j, u, v, a, cap;
        memset(h, -1, sizeof(h));       cc=0;
        for(i=0; i<m; i++){
            scanf(" %d %d %d %d", &u, &v, &a, &cap);
            for(j=1; j<=cap; j++)
                add(u, v, 1, a*(2*j-1));
        }
        add(0, 1, k, 0);
        int mincst, maxflow;
        mcmf(0, n, n+1, mincst, maxflow);
        if(maxflow!=k) printf("-1\n");
        else printf("%d\n", mincst);
    }
    return 0;
}

 




转载于:https://www.cnblogs.com/ramanujan/p/3351176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值