Transportation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3415 Accepted Submission(s): 1464
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
建边 : 1 2 3 4 5 分别对应 1 3 5 7 9的费用,所以只要扩边就好了,建立源点汇点,判断满流即可!
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
const int maxm = 100000;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int,int> P;
//const LL mod = 1e9 + 7;
#define PI 3.1415926
#define sc(x) scanf("%d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%lld\n",x)
#define pfs(x) printf("%d ",x)
#define rep(n) for(int i = 0; i < n; i++)
#define per(n) for(int i = n-1; i >= 0; i--)
#define mem(a,x) memset(a,x,sizeof(a))
int n,m,k;
struct edge
{
int from,to,cap,flow,cost;
edge(int from,int to, int cap, int flow, int cost) : from(from),to(to),cap(cap),flow(flow),cost(cost) {}
};
int tot = 0;
vector<edge> E;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void add_edge(int from, int to, int cap, int cost)
{
E.push_back(edge(from,to,cap,0,cost));
E.push_back(edge(to,from,0,0,-cost));
tot = E.size();
G[from].push_back(tot-2);
G[to].push_back(tot-1);
}
bool spfa(int s, int t, int &flow, int &cost)
{
mem(d,INF);
mem(inq,0);
d[s] = 0,inq[s] = 1; p[s] = 0, a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();Q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++)
{
edge e = E[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u],e.cap - e.flow);
if(!inq[e.to]) {Q.push(e.to); inq[e.to] = 1;}
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s)
{
E[p[u]].flow += a[t];
E[p[u]^1].flow -= a[t];
u = E[p[u]].from;
}
return true;
}
int mcf(int s, int t)
{
int flow = 0,cost = 0;
while(spfa(s,t,flow,cost)) ;
if(flow == k) return cost;
else return -1;
}
int main()
{
int s,t;
int tap[6] = {0,1,3,5,7,9};
while(~scanf("%d%d%d",&n,&m,&k))
{
s = 0;t = n+1;
E.clear();
tot = 0;
for(int i = 0; i <= t; i++) G[i].clear();
add_edge(s,1,k,0);
add_edge(n,t,k,0);
for(int i = 0; i < m; i++)
{
int u,v,w,c;
sc(u);sc(v);sc(w);sc(c);
for(int j = 1; j <= c; j++)
add_edge(u,v,1,tap[j]*w);
}
int ans = mcf(s,t);
pfn(ans);
}
return 0;
}