您的位置:首页 > 其它

HDU-3667 Transportation

2017-12-06 19:45 411 查看
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. 

InputThere 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)
OutputOutput one line for each test case, indicating the minimum cost. If it is impossible to transport all the K units of goods, output -1. 

分析:可以说是一道很裸的最小费用最大流的题目吧.边的容量和费用都给出来了.问题唯一比较难解决的就是那个费用的运算公式:a*f*f,其中a是费用,f是流量。由于流量是最后确定的,因此在跑算法的时候如果直接套公式是没办法操作的,那么这个时候可以参考白书上面的建模方法,对两点u,v之间的边进行拆边,由于容量最大为5,所以最多拆成分别以{1,3,5,7,9}为费用,以1为容量的5条边,由于算法本身包含最小费用性,所以取边肯定时会根据流量从小往大取.

代码:#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int INF=0x3f3f3f3f,maxn=105;
int N,M,K;
struct node
{
int x,y;
};
struct Edge
{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w)
{}
};
vector<node> people,house;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn],p[maxn],d[maxn],Inq[maxn],id;
void init()
{
people.clear();
house.clear();
for(int i=0;i<maxn;i++)
G[i].clear();
edges.clear();
}
void add(int from,int to,int cap,int cost)
{
edges.push_back(Edge{from,to,cap,0,cost});
edges.push_back(Edge{to,from,0,0,-cost});
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int s,int t,int& flow,int& cost)
{
memset(d,INF,sizeof(d));
memset(Inq,0,sizeof(Inq));
d[s]=0,Inq[s]=1,a[s]=INF;
queue<int> q;
q.push(s);
while(!q.empty())
{
int x=q.front();
Inq[x]=0;
q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e = edges[G[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost)
{
d[e.to]=d[x]+e.cost;
a[e.to]=min(a[x],e.cap-e.flow);
p[e.to]=G[x][i];
if(!Inq[e.to])
{
q.push(e.to);
Inq[e.to]=1;
}
}
}
}
if(d[t]==INF)
return false;
flow+=a[t];
cost+=d[t];
for(int u=t;u!=s;u=edges[p[u]].from)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return true;
}
int MinFlow(int s,int t)
{
int flow=0,cost=0;
while(BellmanFord(s,t,flow,cost));
if(flow==K)
return cost;
return -1;
}
void Judge(int u,int v,int a,int c)
{
int j=1;
for(int i=1;i<=c;i++,j+=2)
add(u,v,1,j*a);
}

int main()
{
while(scanf("%d%d%d",&N,&M,&K)!=EOF)
{
int u,v,a,c;
id=N;
init();
for(int i=0;i<M;i++)
{
scanf("%d%d%d%d",&u,&v,&a,&c);
Judge(u,v,a,c);
}
add(0,1,K,0);
printf("%d\n",MinFlow(0,N));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: