首页 技术 正文
技术 2022年11月17日
0 收藏 659 点赞 4,456 浏览 1640 个字
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
const int maxm = 50000*4;
const int maxn = 110;
struct node
{
int v,cost,flow,next;//v表示指向的下一个顶点,a表示系数,flow表示可以过的流量
}edge[maxm];
int head[maxn],dis[maxn],pre[maxn],cur[maxn],vis[maxn],aug[maxn];
int s,t,v,n,m,k,id,flow;
void add_edge(int u,int v,int flow,int cost){
edge[id].v = v;edge[id].cost = cost;edge[id].flow = flow;edge[id].next = head[u];head[u] = id++;
edge[id].v = u;edge[id].cost = -cost;edge[id].flow = 0;edge[id].next = head[v];head[v] = id++;
}
int min(int x,int y){
return x < y ? x : y;
}
void init(){
s = 0,t = n+1;
memset(head,-1,sizeof(head));id = 0;
int u,v,a,flow; while( m-- ){
scanf("%d%d%d%d",&u,&v,&a,&flow);
for(int i = 1; i <= flow; i++)//因为边很少,所以可以拆边
add_edge(u,v,1,i*i*a - a*(i-1)*(i-1));//每增加一条边,费用增加
}
//源点流出的流量为k,汇点流入的流量为k
add_edge(0,1,k,0);
add_edge(n,n+1,k,0);
}
int spfa(){
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(aug,-1,sizeof(aug));
queue<int>que;
aug[s] = (1<<30) -1;
dis[s] = 0;vis[s] = 1;pre[s] = s;
que.push(s);
while( !que.empty()){
int u = que.front();
que.pop();
vis[u] = 0;
for(int id = head[u]; id != -1; id = edge[id].next)
{
int v = edge[id].v;
if( edge[id].flow > 0)
{
int MIN = min(aug[u],edge[id].flow);
if(dis[v] == -1 || dis[v] > dis[u] + edge[id].cost)
{
dis[v] = dis[u] + edge[id].cost;
pre[v] = u;cur[v] = id;
aug[v] = MIN;
// cout << v << " " << aug[v] << endl;
if(!vis[v] ){
vis[v] = 1;
que.push(v);
}
}
}
}
}
if( dis[t] == -1 )return 0;
return 1;
}
int get_max(){ int mincost = 0;
while(spfa() ){
mincost += dis[t];
k -= aug[t];
int now = t;
while( now != s){
edge[cur[now]].flow -= aug[t];
edge[cur[now]^1].flow += aug[t];
now = pre[now];
}
}
if(k > 0)return -1;
return mincost;
}
int main(){
// freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k)){
init();
printf("%d\n",get_max());
}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918