Mining Station on the Sea 【HDU - 2448】【KM算法+多源最短路】

我们两清 提交于 2020-01-01 16:51:06

题目链接


  题意

  给你N个港口,有N艘船,它们分别在不同的矿厂,现在呢,它们要返回港口了,并且每个港口只能停靠一只船,而且呢船只要到了港口就不可以再往外跑了。现在给出K条边,是矿厂与矿厂之间的无向路径,然后呢有P条边,是港口与矿厂之间的无向路径。

  思路

  在这里,我们需要把“进了港口就不能再出来了”这个关键点用好,我们肯定可以确定的是船只能在矿厂和矿厂之间移动,所以我们不妨让起点变成港口,并且再也没有港口的边了,我们直接让它们在矿厂与矿厂之间跑,就可以使得它们确定与该港口之间的最短路了,然后跑KM算法的最小权完美匹配(负边)。即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define Min3(a, b, c) min(a, min(b, c))
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 205;
int N, M, K, P;
struct KM
{
    int n, mp[maxN][maxN], link_x[maxN], link_y[maxN];
    bool vis_x[maxN], vis_y[maxN];
    int que[maxN], top, fail, pre[maxN];
    ll hx[maxN], hy[maxN], slk[maxN];
    bool check(int i)
    {
        vis_x[i] = true;
        if(link_x[i])
        {
            que[fail++] = link_x[i];
            vis_y[link_x[i]] = true;
            return true;
        }
        while(i)
        {
            link_x[i] = pre[i];
            swap(i, link_y[pre[i]]);
        }
        return false;
    }
    void bfs(int S)
    {
        for(int i=1; i<=n; i++)
        {
            slk[i] = INF;
            vis_x[i] = vis_y[i] = false;
        }
        top = 0; fail = 1; que[0] = S;
        vis_y[S] = true;
        while(true)
        {
            ll d = 0;
            while(top < fail)
            {
                for(int i=1, j = que[top++]; i<=n; i++)
                {
                    if(!vis_x[i] && slk[i] >= (d = hx[i] + hy[j] - mp[i][j]))
                    {
                        pre[i] = j;
                        if(d) slk[i] = d;
                        else if(!check(i)) return;
                    }
                }
            }
            d = INF;
            for(int i=1; i<=n; i++)
            {
                if(!vis_x[i] && d > slk[i]) d = slk[i];
            }
            for(int i=1; i<=n; i++)
            {
                if(vis_x[i]) hx[i] += d;
                else slk[i] -= d;
                if(vis_y[i]) hy[i] -= d;
            }
            for(int i=1; i<=n; i++)
            {
                if(!vis_x[i] && !slk[i] && !check(i)) return;
            }
        }
    }
    void Init()
    {
        for(int i=1; i<=n; i++)
        {
            link_x[i] = link_y[i] = 0;
            hy[i] = slk[i] = 0;
            vis_y[i] = false;
        }
        for(int i=1; i<=n; i++)
        {
            hx[i] = 0;
            for(int j=1; j<=n; j++)
            {
                if(hx[i] < mp[i][j]) hx[i] = mp[i][j];
            }
        }
    }
    void solve()
    {
        n = N;
        Init();
        for(int i=1; i<=n; i++) bfs(i);
        ll ans = 0;
        for(int i=1; i<=n; i++) ans += hx[i] + hy[i];
        printf("%lld\n", -ans);
    }
}km;
int id[maxN], _Index, head[maxN], cnt;
struct Eddge
{
    int nex, to, val;
    Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), val(c) {}
}edge[maxN * maxN];
inline void addEddge(int u, int v, int w)
{
    edge[cnt] = Eddge(head[u], v, w);
    head[u] = cnt++;
}
inline void _add(int u, int v, int w) { addEddge(u, v, w); addEddge(v, u, w); }
vector<pair<int, int>> E[maxN];
int dis[maxN];
struct node
{
    int id, val;
    node(int a=0, int b=0):id(a), val(b) {}
    friend bool operator < (node e1, node e2) { return e1.val > e2.val; }
};
priority_queue<node> Q;
inline void Dijkstra(int S)
{
    while(!Q.empty()) Q.pop();
    for(int i=1; i<=M; i++) dis[i] = INF;
    int u, val;
    for(pair<int, int> it : E[S])
    {
        u = it.first; val = it.second;
        Q.push(node(u, val));
        dis[u] = val;
    }
    node now;
    while(!Q.empty())
    {
        now = Q.top(); Q.pop();
        u = now.id;
        if(dis[u] < now.val) continue;
        for(int i=head[u], v; ~i; i=edge[i].nex)
        {
            v = edge[i].to;
            if(dis[v] > dis[u] + edge[i].val)
            {
                dis[v] = dis[u] + edge[i].val;
                Q.push(node(v, dis[v]));
            }
        }
    }
    for(int i=1; i<=N; i++) km.mp[S][i] = -dis[i];
}
inline void init()
{
    _Index = cnt = 0;
    for(int i=1; i<=M; i++)
    {
        id[i] = 0;
        head[i] = -1;
    }
    for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) km.mp[i][j] = -INF;
}
int main()
{
    while(scanf("%d%d%d%d", &N, &M, &K, &P) != EOF)
    {
        init();
        for(int i=1, ID; i<=N; i++)
        {
            scanf("%d", &ID);
            id[ID] = ++_Index;
        }
        for(int i=1; i<=M; i++) if(!id[i]) id[i] = ++_Index;
        for(int i=1, u, v, w; i<=K; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            u = id[u]; v = id[v];
            _add(u, v, w);
        }
        for(int i=1, u, v, w; i<=P; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            v = id[v];
            E[u].push_back(MP(v, w));
        }
        for(int i=1; i<=N; i++) Dijkstra(i);
        km.solve();
    }
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!