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 ;
}

最新文章

  1. GIS简单计算Helper类
  2. php public protected private属性实例详解
  3. Digg Reader 登录不了,原来如此
  4. MailMessage to EML
  5. Hive -- 基于Hadoop的数据仓库分析工具
  6. 根据域名获取IP地址,并探测是否可达
  7. poj 2367 Genealogical tree (拓扑排序)
  8. asp.net framework identity 学习笔记
  9. OpenGL学习-------点、直线、多边形
  10. Oracle 数据库中在使用中文模糊查询时输入中文查询不到结果的解决方法
  11. git使用.gitignore设置不生效或不起作用的问题
  12. 固定footer在底部
  13. 【读书笔记】iOS-设置应用的硬件需求
  14. 05 Zabbix triggers--action--event
  15. LeetCode OJ 47. Permutations II
  16. Android RelativeLayout属性介绍
  17. javascript 鼠标方式去显示
  18. 【Python】多线程2
  19. poj_1390 动态规划
  20. VR与AR的发展趋势分析

热门文章

  1. **stack smashing detecting**
  2. TeleportStone.lua --传送宝石
  3. 反编译dtsi
  4. js监听rem实现响应式
  5. VC运行库合集2005/2008/2010/2012/2013/2015
  6. Hadoop学习14--Hadoop之一点点理解yarn
  7. 在windows上安装scikit-learn开发环境
  8. XE6移动开发环境搭建之IOS篇(2):安装VM9虚拟机(有图有真相)
  9. git 相关
  10. cocos2dx 2.0 CCScrollView的用法以及滑动的原理