hdoj 3667 Transportation 【拆边 + 最小费用最大流】



Transportation

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


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
 



真心激动啊,灵光一闪图就建好了。但是接下来TLE到死,C++死活过不了,G++卡时间才过。。。那么好的心情全毁了。 o(╯□╰)o


题意:有N个城市和M条单向路线。对于每条线路,有四个信息:起点a、终点b、费用系数c、容量d(1<=d<=5),表示(1) 运送x单位的货物需要花费c * x * x,(二) 这条路最多能运送d单位的货物。

现在问你能否将K单位货物从1运送到N,若可以输出最小花费,否则输出-1。



分析:费用流求解问题的前提是 运送x单位货物的花销等于 c *x【c为单位费用】,而这道题的花销则是c * x * x。因此我们需要转化问题,就是把本题计算花销的公式c * x * x变成这样的式子—— c1 * x1 + c2 * x2...


转换:根据等差数列的求和公式可得c * x * x = c * x * x = c * (1 + 3 + ... + 2*x-1) 

拆分式子得到 c*1 + c*3 + ... + c * (2*x-1)——类似表达式c1 * x1 + c2 * x2...


思路:我们把式子的每一项看做是一条容量为1,单位费用为c * (2*x-1)的边 【其中x为边的可行流量即1<=x<=d】。

这样的话就可以把原来容量为d的边拆分成d条容量为1且费用各不相同的边。

拆边后x单位货物经过任意一条边的费用总是为c * x【c为单位费用】,这样就符合了费用流的前提。


最后裸最小费用最大流了。只需要判断最大流量是否满流,若满流输出最小费用,否则输出-1。



AC代码:G++提交,不要用C++。 可能有时卡时间过不了 o(╯□╰)o     勿喷,毕竟我那么渣,代码最快一次才跑920ms。 


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 120
#define MAXM 200000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN], dist[MAXN];
bool vis[MAXN];
int N, M, K;
int source, sink;
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
//    Edge E1 = {u, v, w, 0, c, head[u]};
//    edge[edgenum] = E1;
//    head[u] = edgenum++;
//    Edge E2 = {v, u, 0, 0, -c, head[v]};
//    edge[edgenum] = E2;
//    head[v] = edgenum++;
    edge[edgenum].from = u;
    edge[edgenum].to = v;
    edge[edgenum].cap = w;
    edge[edgenum].flow = 0;
    edge[edgenum].cost = c;
    edge[edgenum].next = head[u];
    head[u] = edgenum++;
    edge[edgenum].from = v;
    edge[edgenum].to = u;
    edge[edgenum].cap = 0;
    edge[edgenum].flow = 0;
    edge[edgenum].cost = -c;
    edge[edgenum].next = head[v];
    head[v] = edgenum++;
}
void getMap()
{
    int a, b, c, d;
    source = 0, sink = N+1;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d%d%d", &a, &b, &c, &d);// 注意输入时 d是容量
        for(int j = 1; j <= 2*d; j+=2)//拆分 每条边容量为1但费用不同
            addEdge(a, b, 1, c*j);//建边时要注意 容量 和 费用不要写反了
    }
    addEdge(source, 1, K, 0);//源点连起点1
    addEdge(N, sink, K, 0);//终点N连汇点
}
bool SPFA(int s, int t)
{
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[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)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}
void MCMF(int s, int t, int &cost, int &flow)
{
    cost = flow = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap - E.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;
    }
}
int main()
{
    while(scanf("%d%d%d", &N, &M, &K) != EOF)
    {
        init();
        getMap();
        int cost, flow;
        MCMF(source, sink, cost, flow);
        if(flow == K)
            printf("%d\n", cost);
        else
            printf("-1\n");
    }
    return 0;
}





你可能感兴趣的:(hdoj 3667 Transportation 【拆边 + 最小费用最大流】)