[Bounds Cost Flow] JZOJ 3302 power supply network

Description

A raccoon peach and live in the world, only one country, there are many cities in this country, every city directly under the jurisdiction of the central government.
Electricity is the only energy in this country, but generation capacity in each city is different, so they produce the lack of electricity in some cities, but the city has some surplus electric power.
Ali, as chief engineer of the State, the power of a raccoon important work is balanced across the country, so that the power in each city are just no surplus or insufficient.
Fortunately, some wires to carry electricity between cities, these wires have their own transport upper and lower limits, and at the same time transmission of electricity will generate a lot of heat.
each wire i heat emitted must be about delivery of electricity no constant term quadratic function, i.e. a_i * x ^ 2 + b_i * x, and since the diode is made of wire, it is clear that only one-way transmitting power per unit of heat needed to cool the unit 1 coins. any between the two cities, there is at most one wire.
Unfortunately, sometimes the electricity network as we thought it would be perfect and in some cases may not in any way Electricity demand enough the whole country. In this case they can only be purchased from some other world power, or the power output to the other world (note that each city can not have a power surplus!), Each city to buy or output power price is not the same (output to spend money).
Because Ali's countries do not have the concept of decimal, transport ,, buy or exchange the power must be based on integer units.
a raccoon's mission is to minimize the cost of gold ( buy / cooling costs + cost of heating the wire sent), he has recently been struggling with this problem do, so there is no time to accompany peach play, every day is the result of peaches scolded T_T. Fortunately, there you are, universal program ape, Please write a program to help Ali solve this problem now.
 

Input

The first line of two integers, n and m, respectively, and the number of cities to have the number of electrical lines.
The next n lines, each line an integer of 3 [left, in, out], where k denotes the k-th row city information:
left represents the city's surplus (negative number is insufficient) of electricity, -5 <= left <= 5.
in, OUT indicates that the city purchase price of a unit of electricity or out of 0 <= in, out <= 10000.
Finally m rows of six integers, u, v, a, b , L, U k-th row in which the information representing the k-th wires:
u, V respectively indicate the beginning and end of the wire city (only electric power from u delivered to V).
a, B represents a quadratic function of the heating wire and a quadratic term.
L, the U-represents lower and upper bounds of the wires delivering power.

Output

Only one integer representing the smallest gold coins to spend.
 

Sample Input

3 2
1 1 1
-2 20 0
-4 20 0
1 2 1 0 0 10
2 3 0 3 0 10

Sample Output

53 
Sample Explanation:
Urban buy 5 1 unit power, it takes 5
city output power to the city 6 units 2, 36 takes
the city power unit 3 outputs four cities to 3, it takes 12
total cost 53.
 
 

Data Constraint

5% for data satisfies m = 0.
For 30% of the data satisfies n <= 30, L = 0 , a = 0.
For 60% of the data satisfies n <= 50, m <= 200.
100% of the data satisfies n <= 200, m <= 600, u! = v, 1 <= u, v <= n, 1 <= a, b <= 3, 1 <= L <= 10, 1 <= L <= U <= 100, 0 <= in, out <= 100, -5 <= left <= 5.

analysis

Face questions have clearly told this is a network flow, and apparently bounds Cost Flow

We all from s to the point of not even a positive cost 0 upper and lower bounds are side ai, and from all the negative to the point t 0 even a cost lower and upper bounds for the side -ai

This point represents a start there are so many electrical output or require so much electricity

Each point is then connected to the unlimited traffic t, cost-side output from the s point connected to each unlimited traffic, the cost of the input side

Then for wire consumption, this is a quadratic function, so be open to write

Weight will become a + b, 3 * a + b, 5 * a + b, 7 * a + b ......

First lower bound cost added to the list, then in order to prevent TLE, we use a method of dynamic Bordered

Upper and lower bounds network flow stamp this uncle

 

#include <iostream> 
#include <cstdio>
#include <queue>
#include <memory.h>
using namespace std;
const int N=2e2+10;
const int M=6e2+10;
const int Inf=2147483647;
struct Edge {
    int u,v,l,r,a,b;
}e[M];
struct Graph {
    int v,c,w,nx;
}g[10000010];
int cnt=1,list[N],dis[N],vis[N],a[N],I[N][N],f[N];
int s,t,ss,tt;
int n,m,ans;

void Add_Edge(int u,int v,int c,int w) {
    g[++cnt]=(Graph){v,c,w,list[u]};list[u]=cnt;
    g[++cnt]=(Graph){u,0,-w,list[v]};list[v]=cnt;
}

void Add_Pipe(int u,int v,int l,int r,int w) {
    Add_Edge(u,v,r-l,w);
    if (l) a[u]-=l,a[v]+=l,ans+=w*l;
}

bool SPFA() {
    queue<int> q;
    while (!q.empty()) q.pop();
    memset(dis,0x7f,sizeof dis);memset(vis,0,sizeof vis);
    q.push(ss);vis[ss]=1;dis[ss]=0;
    for (int i=1;i<=m;i++)
        if (I[e[i].u][e[i].v]==e[i].l&&e[i].l<e[i].r) {
            e[i].l++;
            Add_Edge(e[i].u,e[i].v,1,(2*e[i].l-1)*e[i].a+e[i].b);
        }
    while (!q.empty()) {
        int u=q.front();q.pop();
        for (int i=list[u];i;i=g[i].nx)
            if (g[i].c&&dis[g[i].v]>dis[u]+g[i].w) {
                dis[g[i].v]=dis[u]+g[i].w;f[g[i].v]=i;
                if (!vis[g[i].v]) q.push(g[i].v);
                vis[g[i].v]=1;
            }
        vis[u]=0;
    }
    return dis[tt]!=0x7f7f7f7f;
}

void MCF() {
    int x=tt;
    while (f[x]) {
        ans+=g[f[x]].w;
        I[g[f[x]^1].v][g[f[x]].v]++;I[g[f[x]].v][g[f[x]^1].v]--;
        g[f[x]].c--;g[f[x]^1].c++;
        x=g[f[x]^1].v;
    }
}

void Dinic() {
    while (SPFA())
        MCF();
}

int main() {
    scanf("%d%d",&n,&m);
    s=0;t=n+1;ss=n+2;tt=n+3;
    Add_Pipe(t,s,0,Inf,0);
    for (int i=1;i<=n;i++) {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        if (a>0) Add_Pipe(s,i,a,a,0); else Add_Pipe(i,t,-a,-a,0);
        Add_Pipe(s,i,0,Inf,b);Add_Pipe(i,t,0,Inf,c);
    }
    for (int i=1;i<=m;i++) {
        scanf("%d%d%d%d%d%d",&e[i].u,&e[i].v,&e[i].a,&e[i].b,&e[i].l,&e[i].r);
        Add_Pipe(e[i].u,e[i].v,e[i].l,e[i].l,0);
        ans+=e[i].a*e[i].l*e[i].l+e[i].b*e[i].l;
        I[e[i].u][e[i].v]+=e[i].l;I[e[i].v][e[i].u]-=e[i].l;
    }
    for (int i=s;i<=t;i++) if (a[i]<0) Add_Pipe(i,tt,0,-a[i],0); else Add_Pipe(ss,i,0,a[i],0);
    Dinic();
    printf("%d",ans);
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11145008.html