HDU 2448 Mining Station on the Sea 最短路+KM
2015-12-18 19:06
435 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2448
题意:有n个港口n条船,m个采矿站,船只能在能够通信的采矿站之间或者能够通信的港口和采矿站之间航行,给你能够通信的单位之间的距离,现在有n条船全都要从采矿站返回港口,每个港口只能容纳一条船且船进去后不能出来,求n条船航行的距离最小和。
思路:对于每条船求一次最短路,然后建二分图,它和每个港口的最短距离就是就是权值。注意采矿站之间是双向的,而港口之间是单向的,因为港口只能进不能出。
总结:弱菜想了好久,对于采矿站、港口和能够通信的采矿站分别建图,WA一次,然后各种思路,都被自己推翻了,没想到结果竟如此简单。。。
题意:有n个港口n条船,m个采矿站,船只能在能够通信的采矿站之间或者能够通信的港口和采矿站之间航行,给你能够通信的单位之间的距离,现在有n条船全都要从采矿站返回港口,每个港口只能容纳一条船且船进去后不能出来,求n条船航行的距离最小和。
思路:对于每条船求一次最短路,然后建二分图,它和每个港口的最短距离就是就是权值。注意采矿站之间是双向的,而港口之间是单向的,因为港口只能进不能出。
总结:弱菜想了好久,对于采矿站、港口和能够通信的采矿站分别建图,WA一次,然后各种思路,都被自己推翻了,没想到结果竟如此简单。。。
#include <iostream> #include <string> #include <cstdio> #include <algorithm> #include <cstring> #include <cctype> #include <vector> #include <cmath> #include <queue> using namespace std; const int N = 1010; const int INF = 0x3f3f3f3f; int nx, ny; int lx , ly , slack , match , s ; bool visx , visy ; int n, m; int v ; int mp ; int dist ; bool used ; bool hungary(int v) { visx[v] = true; for(int i = 1; i <= ny; i++) { if(visy[i]) continue; if(lx[v] + ly[i] == s[v][i]) { visy[i] = true; if(match[i] == -1 || hungary(match[i])) { match[i] = v; return true; } } else slack[i] = min(slack[i], lx[v] + ly[i] - s[v][i]); } return false; } void km() { memset(match, -1, sizeof match); memset(ly, 0, sizeof ly); for(int i = 1; i <= nx; i++) lx[i] = -INF; for(int i = 1; i <= nx; i++) for(int j = 1; j <= ny; j++) lx[i] = max(lx[i], s[i][j]); for(int i = 1; i <= nx; i++) { memset(slack, 0x3f, sizeof slack); while(true) { memset(visx, 0, sizeof visx); memset(visy, 0, sizeof visy); if(hungary(i)) break; else { int d = INF; for(int j = 1; j <= ny; j++) if(!visy[j]) d = min(d, slack[j]); for(int j = 1; j <= nx; j++) if(visx[j]) lx[j] -= d; for(int j = 1; j <= ny; j++) if(visy[j]) ly[j] += d; else slack[j] -= d; } } } } void spfa(int s) { memset(dist, 0x3f, sizeof dist); memset(used, 0, sizeof used); queue <int> que; que.push(s); dist[s] = 0; used[s] = true; while(! que.empty()) { int v = que.front(); que.pop(); for(int i = 1; i <= n + m; i++) { if(dist[i] > dist[v] + mp[v][i]) { dist[i] = dist[v] + mp[v][i]; if(used[i] == false) { que.push(i); used[i] = true; } } } used[v] = false; } } int main() { int k, p; int a, b, c; while(~ scanf("%d%d%d%d", &n, &m, &k, &p)) { for(int i = 1; i <= n; i++) scanf("%d", &v[i]); memset(mp, 0x3f, sizeof mp); for(int i = 1; i <= k; i++) { scanf("%d%d%d", &a, &b, &c); if(mp[a][b] > c) mp[a][b] = mp[b][a] = c; } for(int i = 1; i <= p; i++) { scanf("%d%d%d", &a, &b, &c); if(mp[b][a+m] > c) mp[b][a+m] = c; } for(int i = 1; i <= n; i++) { spfa(v[i]); for(int j = m + 1; j <= n + m; j++) s[i][j-m] = -dist[j]; } nx = n, ny = n; km(); int res = 0; for(int i = 1; i <= n; i++) res += s[match[i]][i]; printf("%d\n", -res); } return 0; }
相关文章推荐
- LeetCode Merge Two Sorted Lists
- 用OC语言实现贪吃蛇小游戏
- [BZOJ1096] [ZJOI2007]仓库建设
- 【jQuery】使用ajax()方法加载服务器数据
- 【工作日志】0004工作日志
- poj 3228 Gold Transportation 二分+网络流
- 第三方支付架构设计之―帐户体系
- 关于图像对比度【4】——对比度调整强度
- adb 和 fastboot 的基本用法详解
- android edittext 点击两次才弹出输入法的解决方法之一
- 总结❤
- 利用java UDP协议,实现简单的双人聊天
- HDU-1075-What Are You Talking About(字典树&map)
- hibernate 实现多表连接查询(转载)
- Swift基础--使用TableViewController自定义列表
- C++ thrift详细教程 及和Protobuf对比
- 【jQuery】使用serialize()方法序列化表单元素值
- VS读取文件或写入文件时出现中文乱码问题
- the begining of coding
- C语言结构体 别名定义