HDU 2448 — Mining Station on the Sea 最优匹配+Floyd
来源:互联网 发布:ui切图软件 编辑:程序博客网 时间:2024/06/04 19:37
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2448
题意:
第一行给出n、m、k、p;
n表示有n个港口和n艘船;m表示有m个油田;k表示油田之间的k条无向边;p表示港口到油田的p条有向边;
第二行给出n艘船所在油田的位置;
下面k行给出k条边的信息;接下去p行给出p条边的信息;
问将这n艘船开回港口(一个港口最多容纳一艘船)的最短路程;
思路:
floyd求出港口和油田间的最短距离,在港口和船所在的油田之间建二分图,KM算法求最优匹配;
#include<stdio.h> #include<string.h> #include<algorithm> #define inf 1e9 using namespace std; const int maxn = 510;int boat[maxn], dis[maxn][maxn];int nx, ny; int match[maxn]; int lx[maxn], ly[maxn], slack[maxn], w[maxn][maxn];bool visx[maxn], visy[maxn]; bool dfs(int x) { visx[x] = true; for(int y = 1;y<=ny;y++) { if(visy[y]) continue; int t = lx[x]+ly[y]-w[x][y]; if(t == 0) { visy[y] = true; if(match[y] == 0 || dfs(match[y])) { match[y] = x; return true; } } else if(slack[y]>t) slack[y] = t; } return false; } int KM() { memset(ly, 0, sizeof ly); memset(match, 0, sizeof match); for(int i = 1;i<=nx;i++) { lx[i] = -inf; for(int j = 1;j<=ny;j++) lx[i] = max(lx[i], w[i][j]); } for(int x = 1;x<=nx;x++) { for(int y = 1;y<=ny;y++) slack[y] = inf; while(1) { memset(visx, false, sizeof visx); memset(visy, false, sizeof visy); if(dfs(x)) break; int d = inf; for(int i = 1;i<=ny;i++) { if(!visy[i]) d = min(d, slack[i]); } for(int i = 1;i<=nx;i++) { if(visx[i]) lx[i]-=d; } for(int i = 1;i<=ny;i++) { if(visy[i]) ly[i]+=d; else slack[i]-=d; } } } int res = 0; for(int i = 1;i<=ny;i++) { if(match[i]) res+=w[match[i]][i]; } return -res; } void Floyd(int n){for(int k = 1;k<=n;k++)for(int i = 1;i<=n;i++)for(int j = 1;j<=n;j++)dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);}int main(){int n, m, k, p;while(scanf("%d%d%d%d", &n, &m, &k, &p)!=EOF){for(int i = 1;i<=n;i++)scanf("%d", &boat[i]);for(int i = 1;i<=n+m;i++){for(int j = 1;j<=n+m;j++){if(i == j)dis[i][j] = 0;elsedis[i][j] = inf;}}while(k--){int u, v, val;scanf("%d%d%d", &u, &v, &val);dis[u+n][v+n] = dis[v+n][u+n] = val;//油田之间双向 }while(p--){int u, v, val;scanf("%d%d%d", &u, &v, &val);dis[u][v+n] = val;//港口到油田单向 }Floyd(n+m);for(int i = 1;i<=n;i++)for(int j = 1;j<=n;j++)w[i][j] = -dis[i][boat[j]+n];//取负值 nx = ny = n;int ans = KM();printf("%d\n", ans);}return 0;}
0 0
- HDU 2448 Mining Station on the Sea(Floyd+最优匹配)
- HDU 2448 Mining Station on the Sea(Floyd+最优匹配)
- HDU 2448 Mining Station on the Sea(Floyd+最优匹配)
- HDU 2448 — Mining Station on the Sea 最优匹配+Floyd
- HDU 2448 Mining Station on the Sea(floyd+KM)
- HDU 2448 Mining Station on the Sea(KM最大匹配+floyd)
- hdu 2448 Mining Station on the Sea【最短路Floyd+KM 最佳匹配】
- hdu 2448 Mining Station on the Sea
- HDU 2448 Mining Station on the Sea
- 【KM匹配】 HDOJ 2448 Mining Station on the Sea
- hdu 2448 Mining Station on the Sea(KM匹配//费用流)
- hdu 2448 Mining Station on the Sea //floyed+km算法
- 【HDU】 2448 Mining Station on the Sea 费用流
- HDU 2448 Mining Station on the Sea(费用流)
- HDU 2448 Mining Station on the Sea 费用流
- hdu 2448 Mining Station on the Sea【网络费用流】
- Mining Station on the Sea (hdu 2448 SPFA+KM)
- HDU 2448 Mining Station on the Sea 最短路+KM
- 9秒学院Html5游戏:一场可预见的疯狂
- Java学习笔记------一对一聊天功能----
- Android Intent IntentFilter
- 《游戏引擎架构》读书笔记(三)
- ARM Linux 3.x的设备树(Device Tree)
- HDU 2448 — Mining Station on the Sea 最优匹配+Floyd
- 11882 - Biggest Number(DFS + 剪枝)
- 字符串工具类
- char/byte/short类型的加法和类型转换问题
- 修改Android studio背景主题
- POJ---1080-Human Gene Functions
- doget与dopost
- Android eclipse 提示java代码 快捷键
- 【基础练习】【背包DP】codevs1068 乌龟棋题解