hdu 3667 拆边加最小费用流

Transportation

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

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
这题的重点是拆边,虽然之前没见过拆边的题目,但是这提这么简单的思路我竟然没想到,还是该给自己一个大耳光
 
 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<climits>
#define MAXP 110
#define MAXE 51000
using namespace std;
struct Edge
{
int s,t,f,c,next;
} edge[MAXE];
int head[MAXP];
int pre[MAXP];
int dist[MAXP];
bool isq[MAXP];
int n,m,k,s,t,u,v,c,f,ent;
void add(int S,int T,int f,int c)
{
edge[ent].s=S;
edge[ent].t=T;
edge[ent].f=f;
edge[ent].c=c;
edge[ent].next=head[S];
head[S]=ent++;
edge[ent].s=T;
edge[ent].t=S;
edge[ent].f=;
edge[ent].c=-c;
edge[ent].next=head[T];
head[T]=ent++;
}
int MIN(int a,int b)
{
return a<b?a:b;
}
bool spfa()
{
memset(pre,-,sizeof(pre));//初始化路径为-1
for(int i=s; i<=t; i++)
{
isq[i]=false;//每点开始时均不在队列里面
dist[i]=INT_MAX;//初始化到每点的最小费用均为INT_MAX
}
queue<int>q;
q.push(s);
isq[s]=true;//源点s已经放入到队列里面去了
dist[s]=;//从源点到源点的距离为0
while(!q.empty())//当队列为空时优化过程结束,退出循环
{
int temp1=q.front();
q.pop();
isq[temp1]=false;//该点已经退出队列
for(int i=head[temp1]; i!=-; i=edge[i].next) //从该点找通过邻接表找所有的以该点为起点的边,从中找出能优化的点
{
int temp2=edge[i].t;
if(edge[i].f&&dist[temp2]>dist[temp1]+edge[i].c)
{
dist[temp2]=dist[temp1]+edge[i].c;
pre[temp2]=i;
if(!isq[temp2])//如果该点不在队列中,则将该点放入队列中
{
q.push(temp2);
isq[temp2]=true;
}
}
}
}
return pre[t]!=-;//如果pre[t]==-1的话说明没有找到从s到t的路径,即已经找到所有的路径了,结束循环
}
void mcmf()
{
int tot=;
int sum=;
int mincost=INT_MAX;
int minn=INT_MAX;
while(spfa())
{
tot++;
mincost=dist[t];
sum+=mincost;
if(tot==k)
{
printf("%d\n",sum);
return;
}
for(int i=pre[t];i!=-;i=pre[i])//最小费用最大流中的减流的过程
{
edge[i].f--;
edge[i^].f++;
i=edge[i].s;
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
ent=;
memset(head,-,sizeof(head));
s=;t=n;
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&c,&f);
for(int j=;j<=f;j++)
add(u,v,,(j*-)*c);
}
if(m==||n==)
{
printf("0\n");
continue;
}
mcmf();
}
return ;
}
上一篇:读书分享全网学习资源大合集,推荐Python学习手册等三本书「01」


下一篇:ActiveMQ_Mqtt的TCP丢包