`

HDU 3667 Transportation

    博客分类:
  • ACM
c 
阅读更多

Transportation

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


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
 

 

题意:

用 0 到 n-1 运送k 个货物,m条边,每条边,u,v,w,c,;  每条边表示 运送 x 单元货物的花费为  w * x*x,c 表示最大流量 ,

求 最小费用  ,若不能全部运送输出 -1;

 

题解:  拆边 ,看到这个题直接的思路就是费用流,一开始提了个模版 上去 发现不对,我们以前做的  花费是  w*f  ,而现在是  w*f*f(此时 以 最短路 已不能找到正确的答案);

所以我们要变为  w*f  ,那么就是,我们 发现 第一次运送时

费用为w,第二次取这条路时费用为3w(即流量为2时费用值为w+3w=4w),……,第i次取这条路时费用为(2*i-1)*w

在   u  到  u  连  c  条边,每条边的流量 为 1 ,费用   (2*i - 1)*w

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

const int VM=110;
const int EM=501000;
const int INF=0x3f3f3f3f;

struct Edge{
    int u,v,nxt;
    int flow,cost;
}edge[EM<<1];

int n,m,k,cnt,head[VM];
int pre[VM],dis[VM],vis[VM];

void addedge(int cu,int cv,int cw,int cc){
    edge[cnt].u=cu;  edge[cnt].v=cv;  edge[cnt].flow=cw;
    edge[cnt].cost=cc;  edge[cnt].nxt=head[cu];  head[cu]=cnt++;

    edge[cnt].u=cv;  edge[cnt].v=cu;  edge[cnt].flow=0;
    edge[cnt].cost=-cc;  edge[cnt].nxt=head[cv];  head[cv]=cnt++;
}

int src,des;

int SPFA(){
    queue<int> q;
    while(!q.empty())
        q.pop();
    for(int i=0;i<=des;i++){
        dis[i]=INF;
        vis[i]=0;
        pre[i]=-1;
    }
    dis[src]=0;
    vis[src]=1;
    q.push(src);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].v;
            if(edge[i].flow>0 && dis[v]>dis[u]+edge[i].cost){
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return pre[des]!=-1;
}

int flow,cost;

void mincost(){
    flow=0,cost=0;
    while(SPFA()){
        int u=des,minf=INF;
        while(u!=src){
            minf=min(minf,edge[pre[u]].flow);
            u=edge[pre[u]].u;
        }
        u=des;
        while(u!=src){
            edge[pre[u]].flow-=minf;
            edge[pre[u]^1].flow+=minf;
            u=edge[pre[u]].u;
        }
        cost+=minf*minf*dis[des];
        flow+=minf;
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d%d",&n,&m,&k)){
        cnt=0;
        memset(head,-1,sizeof(head));
        int u,v,w,c;
        for(int i=0;i<m;i++){
            scanf("%d%d%d%d",&u,&v,&w,&c);
            for(int j=0;j<c;j++)
                addedge(u,v,1,w*(2*j+1));
        }
        addedge(0,1,k,0);
        src=0;  des=n;
        mincost();
        if(flow<k)
            printf("-1\n");
        else
            printf("%d\n",cost);
    }
    return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics