HDU_2448_Mining Station on the Sea(最短路 + 最小费用流)

时间:2022-11-22 04:32:09

Mining Station on the Sea

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2831    Accepted Submission(s): 860



Problem Description The ocean is a treasure house of resources and the development of human society comes to depend more and more on it. In order to develop and utilize marine resources, it is necessary to build mining stations on the sea. However, due to seabed mineral resources, the radio signal in the sea is often so weak that not all the mining stations can carry out direct communication. However communication is indispensable, every two mining stations must be able to communicate with each other (either directly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land to get put into use, there build n ports correspondently along the coast and every port can communicate with one or more mining stations directly.

Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.

The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.

Notice that once the ship entered the port, it will not come out!
 
Input There are several test cases. Every test case begins with four integers in one line, n (1 = <n <= 100), m (n <= m <= 200), k and p. n indicates n vessels and n ports, m indicates m mining stations, k indicates k edges, each edge corresponding to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.

 
Output Each test case outputs the minimal total sum of their sailing routes.
 
Sample Input
3 5 5 6
1 2 4
1 3 3
1 4 4
1 5 5
2 5 3
2 4 3
1 1 5
1 5 3
2 5 3
2 4 6
3 1 4
3 2 2
 
Sample Output
13
 

题意:有m个油田、n条渔船以及n个港口,n条船分别停在n个油田上,给出油田与油田之间的距离,油田与港口之间的距离,现在你要把船开回到港口去,一个港口只能容纳一条船,问所有船行驶的总路程最少是多少。

分析:最短路 + 最小费用流。

构图:

加入超级源点s(0),从s指向每条船 i 连一条边,容量为1,费用为0;

加入超级汇点t(2 * n + 1),从每个港口 i + n 指向t连边,容量为1,费用为0;

最短路求出每条船到每个港口的最短距离,从该条船向每个港口连边,容量为最短路径长度。

最后跑一遍最小费用最大流即可。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2448

代码清单:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <cctype>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 200 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w) : from(u), to(v), cap(c), flow(f), cost(w) {}
};

struct MCMF{
int n, m, flow, cost;
vector <Edge> edge; //边数的两倍
vector <int> G[maxn]; //邻接表,G[i][j]表示i的第j条边在e数组中的序号
int inq[maxn]; //是否在队列
int d[maxn]; //Bellman-Ford
int p[maxn]; //上一条弧
int a[maxn]; //可改进量

void init(int n){
this -> n = n;
for(int i = 0; i <=n ; i++) G[i].clear();
edge.clear();
}

void addEdge(int from, int to, int cap, int cost){
edge.push_back(Edge(from, to, cap, 0, cost));
edge.push_back(Edge(to, from, 0, 0, -cost));
m = edge.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BellmanFord(int s, int t, int& flow, int& cost){
memset(d, INF, sizeof(d));
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;

queue <int> q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++){
Edge& e = edge[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]){
q.push(e.to);
inq[e.to] = 1;
}
}
}
}

if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
for(int u = t; u != s; u = edge[p[u]].from){
edge[p[u]].flow += a[t];
edge[p[u]^1].flow -= a[t];
}
return true;
}

//需要保证初始网络中没有负权圈
void MincostMaxflow(int s, int t){
flow = 0, cost = 0;
while(BellmanFord(s, t, flow, cost));
}
};

const int maxb = 100 + 5;
const int maxg = 300 + 5;

struct EDGE{
int to,dis;
EDGE(){}
EDGE(int _to, int _dis) : to(_to), dis(_dis) {}
};

int n, m, k, p;
int a, b, c;
int sboat[maxb];
bool vis[maxg];
int dist[maxg];
vector <EDGE> graph[maxg];

int tail;
MCMF mcmf;

void initf(){
for(int i = 1; i <= maxg; i++){
graph[i].clear();
}
}

void input(){
for(int i = 1; i <= n; i++){
scanf("%d", &sboat[i]);
}
for(int i = 1; i <= k; i++){
scanf("%d%d%d", &a, &b, &c);
graph[a].push_back(EDGE(b, c));
graph[b].push_back(EDGE(a, c));
}
for(int i = 1; i <= p; i++){
scanf("%d%d%d", &a, &b, &c);
graph[b].push_back(EDGE(a + m, c));
}
}

void spfa(int s){
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
queue <int> q;
while(!q.empty()) q.pop();
dist[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = false;
for(int i = 0; i < graph[u].size(); i++){
EDGE& e = graph[u][i];
if(dist[e.to] > dist[u] + e.dis){
dist[e.to] = dist[u] + e.dis;
if(!vis[e.to]){
vis[e.to] = true;
q.push(e.to);
}
}
}
}
}

void createGraph(){
tail = n + n + 1;
mcmf.init(tail + 1);

for(int i = 1; i <= n; i++){
mcmf.addEdge(0, i, 1, 0);
mcmf.addEdge(i + n, tail, 1, 0);
}
for(int i = 1; i <= n; i++){
spfa(sboat[i]);
for(int j = m + 1; j <= m + n; j++){
if(dist[j] != INF) mcmf.addEdge(i, j - m + n, 1, dist[j]);
}
}
}

void solve(){
createGraph();
mcmf.MincostMaxflow(0, tail);
printf("%d\n", mcmf.cost);
}

int main(){
while(scanf("%d%d%d%d", &n, &m, &k, &p) != EOF){
initf();
input();
solve();
}end();
}