★ HDU 3667 费用与流量平方成正比的最小流

Transportation

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


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 a i. If you want to carry x units of goods along this road, you should pay a i * x 2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound C i, which means that you cannot transport more than C i 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 (u i, v i, a i, C i), indicating there is a directed road from city u i to v i, whose coefficient is a i and upper bound is C i. (1 <= u i, v i<= N, 0 < a i <= 100, C i <= 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
 

分析:一个模型,只需将每条边拆成容量为1,权值为等差数列的若干条边即可,然后直接上模板。。。

代码:

//O(Kn^2m)
//如果要求最大费用的话 只需在加边的时候加-的边  输出时输出-ans即可
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;   //记得必要的时候改成无符号
const int maxn=200;
const int maxm=1000005;
const int INF=1000000000;
struct EdgeNode{
    int from;
    int to;
    int flow;
    int cost;
    int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z,int c)
{
edge[cnt].from=x;edge[cnt].to=y;edge[cnt].flow=z;edge[cnt].cost=c;edge[cnt].next=head[x];head[x]=cnt++;
edge[cnt].from=y;edge[cnt].to=x;edge[cnt].flow=0;edge[cnt].cost=-c;edge[cnt].next=head[y];head[y]=cnt++;
//printf("%d %d %d %d\n",x,y,z,c);
}
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
int S,T,n,m;
int d[maxn],in[maxn],pre[maxn];
queue<int>Q;
bool spfa(int S,int T)
{
    int u,v,f,c;
    while(!Q.empty())Q.pop();
    memset(in,0,sizeof(in));
    for(int i=0;i<=n;i++)d[i]=INF;
    d[S]=0;
    Q.push(S);
    while(!Q.empty())
    {
        u=Q.front(); Q.pop(); in[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            v=edge[i].to; f=edge[i].flow; c=edge[i].cost;
            if(f&&d[u]+c<d[v]){
                d[v]=d[u]+c; pre[v]=i;
                if(!in[v]){
                    in[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(d[T]==INF)return false;
    return true;
}
int MCMF(int S,int T,int k)
{
    int u;
    int max_flow=0;
    int min_cost=0;
    while(spfa(S,T))
    {
        if(max_flow==k)break;
        int flow=INF;
        u=T;
        while(u!=S){
            flow=min(flow,edge[pre[u]].flow);
            u=edge[pre[u]].from;
        }
        u=T; max_flow+=flow; min_cost+=d[T]*flow;
        while(u!=S){
            edge[pre[u]].flow-=flow;
            edge[pre[u]^1].flow+=flow;
            u=edge[pre[u]].from;
        }
    }
    if(max_flow<k)return -1;
    return min_cost;
}

struct q
{
    int x,y,z,c;
}a[5005];

int main()
{
    int k,i,j,x,y,z,c;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        init();
        for(i=1;i<=m;i++)
            scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].c,&a[i].z);
        for(i=1;i<=m;i++){
            c=-a[i].c; x=a[i].x; y=a[i].y; z=1;
            for(j=1;j<=a[i].z;j++){
                c+=2*a[i].c;
                add(x,y,z,c);
            }
        }
        S=1; T=n;
        printf("%d\n",MCMF(S,T,k));
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值