题目:http://acm.hdu.edu.cn/showproblem.php?pid=4411

  1. floyd处理出最短路
  2. 每个点拆为i、i+n,i到i+n连一条容量为1,费用为负无穷的边,代表这个城市必须访问
  3. i+n到j(j>i)分别建边,容量为1,费用为i、j最短路
  4. i+n到汇点建立容量为1费用为g[0][i]的边,代表会警察局
  5. 0到i建立容量为1费用为g[0][i]的边
  6. 0到汇点建立容量为k,费用为0的边,代表有的警察可能一直待在警察局
  7. 源点到0建立容量为k费用为0的边

ps:这题会有重边,以后一定要注意,每道题都要判重!

 #include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxv = ;
typedef pair<int,int> P;
struct edge{
int to,cap,cost,rev;
};
int V;
vector<edge> g[maxv];
int h[maxv];
int dist[maxv];
int prevv[maxv],preve[maxv];
void addedge(int from,int to,int cap,int cost){
g[from].push_back(edge{to,cap,cost,g[to].size()});
g[to].push_back((edge{from,,-cost,g[from].size()-}));
}
int solve(int s,int t,int f){
int res = ;
memset(h,,sizeof(h)) ;
while(f > ){
priority_queue<P,vector<P>,greater<P> > que;
memset(dist,inf,sizeof(dist));
dist[s] = ;
que.push(P(,s));
while(!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
if(dist[v] < p.first)
continue;
for(int i = ;i<g[v].size();i++){
edge &e = g[v][i] ;
if(e.cap> && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t] == inf)
return -;
for(int i = ;i<V;i++)
h[i] += dist[i];
int d = f;
for(int v = t;v!=s;v=prevv[v])
d = min(d,g[prevv[v]][preve[v]].cap);
f -= d;
res += d*h[t];
for(int v = t;v!=s;v = prevv[v]){
edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return res;
}
int mp[][];
int main(){
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)){
if(!n && !m && !k)
break;
V = *n+;
int s = *n+,t = *n+;
for(int i = ;i<*n+;i++)
g[i].clear();
memset(mp,inf,sizeof(mp));
for(int i = ;i<=n;i++)
mp[i][i] = ;
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
mp[u][v] = mp[v][u] = min(mp[u][v],w);
}
for(int k = ;k<=n;k++)
for(int i = ;i<=n;i++)
for(int j = ;j<=n;j++)
mp[i][j] = min(mp[i][j],mp[i][k]+mp[k][j]);
for(int i = ;i<=n;i++){
addedge(,i,,mp[][i]);
addedge(i+n,t,,mp[][i]);
addedge(i,i+n,,-);
for(int j = i+;j<=n;j++){
if(mp[i][j] != inf)
addedge(i+n,j,k,mp[i][j]);
}
}
addedge(s,,k,);
addedge(,t,k,);
cout << solve(s,t,k)+n* << endl;
}
return ;
}

HDU4411 最小费用流的更多相关文章

  1. POJ2195 最小费用流

    题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...

  2. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

  3. POJ 2516:Minimum Cost(最小费用流)

    https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...

  4. POJ-2175 Evacuation Plan 最小费用流、负环判定

    题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...

  5. Going Home (hdu 1533 最小费用流)

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

  6. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  7. [haoi2010]订货 最小费用流

    这道题oj上的标签是动态规划,但我想不出来动态规划怎么搞,空间不爆,时间也要爆的: 好的,不扯淡,此题正常做法是最小费用流: 这道题我写了两遍,为什么呢?原因是第一次写的时候,不会写费用流,又恰好没带 ...

  8. FZU2143Board Game(最小费用流)

    题目大意是说有一个B矩阵,现在A是一个空矩阵(每个元素都为0),每次操作可以将A矩阵相邻的两个元素同时+1,但是有个要求就是A矩阵的每个元素都不可以超过K,求 这个的最小值 解题思路是这样的,新建起点 ...

  9. POJ 2516 Minimum Cost 最小费用流

    题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...

随机推荐

  1. OSG 3.40编译,osgQt编译失败解决方案

    osgQt编译不出来,主要原因在于cmake配置不正确. 第一步:修改CMakeList.txt文件,在文件开始加入两行 " CACHE STRING "") set(C ...

  2. 解读ASP.NET 5 & MVC6系列(11):Routing路由

    新版Routing功能介绍 在ASP.NET 5和MVC6中,Routing功能被全部重写了,虽然用法有些类似,但和之前的Routing原理完全不太一样了,该Routing框架不仅可以支持MVC和We ...

  3. [LeetCode] Peeking Iterator 顶端迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  4. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  5. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. mysqld初探

    一.简介 deamon是守护神的意思,表示守护进程.mysqld就是mysql的服务器端,就是基于socket的一个服务器端程序,它始终监听3306端口(默认端口).mysql是客户端程序. 安装my ...

  8. c#常用方法

    创建目录 Directory.CreateDirectory(Application.StartupPath+"\\test");

  9. Redis的三种启动方式

    转载:http://www.tuicool.com/articles/aQbQ3u Part I. 直接启动 下载 官网下载 安装 tar zxvf redis-2.8.9.tar.gz cd red ...

  10. [webpack] 配置react+es6开发环境

    写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...