当先锋百科网

首页 1 2 3 4 5 6 7

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

题意:有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[N], ly[N], slack[N], match[N], s[N][N];
bool visx[N], visy[N];
int n, m;
int v[N];
int mp[N][N];
int dist[N];
bool used[N];

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;
}