Mining Station on the Sea

题目描述

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!

 

 

输入

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.

 

输出

Each test case outputs the minimal total sum of their sailing routes.

 

样例输入

复制样例数据

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

样例输出

13

 

KM算法,求出n个点的最短路,变成二分图最优匹配,套用KM模板即可。

最短路也可以floyd求。

最优匹配应该也可以用费用流求解

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int modp=998244353;
const int maxn=1e6+50;
const double eps=1e-6;
#define lowbit(x)  x&(-x)
#define INF 0x3f3f3f3f
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    return (x>0)?1:-1;
}
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
ll qmod(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=(ans*a)%mod;
        }
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int nx,ny;
int g[310][310],w[310][310];
int linker[310],lx[310],ly[310];
int slack[310];
bool visx[310],visy[310];
int n,m,k,p;
int st[310],v[310],d[310];
void dij(int s,int n){
    memset(v,0,sizeof(v));
    memset(d,INF,sizeof(d));
    d[s]=0;
    for(int i=0;i<n;i++){
        int x,m=INF;
        for(int y=0;y<n;y++){
            if(!v[y]&&d[y]<=m){
                m=d[x=y];
            }
        }
        v[x]=1;
        for(int y=0;y<n;y++){
            d[y]=min(d[y],d[x]+w[x][y]);
        }
    }
}

bool dfs(int x){
    visx[x]=true;
    for(int y=0;y<ny;y++){
        if(visy[y]){
            continue;
        }
        int tmp=lx[x]+ly[y]-g[x][y];
        if(tmp==0){
            visy[y]=true;
            if(linker[y]==-1||dfs(linker[y])){
                linker[y]=x;
                return true;
            }
        }
        else if(slack[y]>tmp){
            slack[y]=tmp;
        }
    }
    return false;
}
int KM(){
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(int i=0;i<nx;i++){
        lx[i]=-INF;
        for(int j=0;j<ny;j++){
            if(g[i][j]>lx[i]){
                lx[i]=g[i][j];
            }
        }
    }
    for(int x=0;x<nx;x++){
        for(int i=0;i<ny;i++){
            slack[i]=INF;
        }
        while(1){
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x)){
                break;
            }
            int d=INF;
            for(int i=0;i<ny;i++){
                if(!visy[i]&&d>slack[i]){
                    d=slack[i];
                }
            }
            for(int i=0;i<=nx;i++){
                if(visx[i]){
                    lx[i]-=d;
                }
            }
            for(int i=0;i<ny;i++){
                if(visy[i]){
                    ly[i]+=d;
                }
                else{
                    slack[i]-=d;
                }
            }
        }
    }
    int res=0;
    for(int i=0;i<ny;i++){
        if(linker[i]!=-1){
            res+=g[linker[i]][i];
        }
    }
    return res;
}
int main()
{
    while(~scanf("%d %d %d %d",&n,&m,&k,&p)){
        for(int i=0;i<n;i++){
            scanf("%d",&st[i]);
            st[i]--;
        }
        memset(w,INF,sizeof(w));
        memset(g,0,sizeof(g));
        for(int i=0;i<k;i++){
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            w[x-1][y-1]=z;
            w[y-1][x-1]=z;
        }
        for(int i=0;i<p;i++){
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            w[y-1][x-1+m]=z;
        }
        for(int i=0;i<n;i++){
            dij(st[i],m+n);
            for(int j=m;j<m+n;j++){
                if(d[j]<INF){
                    g[i][j-m]=-d[j];
                }
            }
        }
        nx=n;
        ny=n;
        printf("%d\n",-KM());
    }
    return 0;
}

转自:https://www.xuebuyuan.com/zh-hant/552993.html

#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 3000
#define MAXM 300000
using namespace std;
struct node
{
    int u,v,f,c;
};
node e[MAXM];
int first[MAXN],nex[MAXM],cc;
int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN];
inline void add_edge(int u,int v,int f,int c)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    nex[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    nex[cc]=first[v];
    first[v]=cc;
    cc++;
}
int SPFA(int s,int t)
{
    memset(inq,0,sizeof(inq));
    memset(d,inf,sizeof(d));
    memset(pre,-1,sizeof(pre));
    memset(preedge,-1,sizeof(preedge));
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        int i;
        for(i=first[u];i!=-1;i=nex[i])
        {
            int v=e[i].v;
            if(e[i].f)
            {
                if(d[v]>d[u]+e[i].c)
                {
                    d[v]=d[u]+e[i].c;
                    pre[v]=u;
                    preedge[v]=i;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(d[t]>=inf)
        return 0;
    else
        return 1;
}
int Min_Cost_Flow(int s,int t)
{
    int ans_flow=0,ans_cost=0,mm,tmp;
    while(SPFA(s,t))
    {
        mm=inf;
        int u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            mm=min(mm,e[tmp].f);
            u=pre[u];
        }
        u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            e[tmp].f-=mm;
            e[tmp^1].f+=mm;
            u=pre[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*d[t];
    }
    return ans_cost;
}
int main()
{
    int n,m,k,p;
    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
    {
        memset(first,-1,sizeof(first));
        memset(nex,-1,sizeof(nex));
        cc=0;
        int i;
        int s=0,t=n+m+1;
        for(i=1;i<=n;i++)
        {
            add_edge(i+m,t,1,0);
            int a;
            scanf("%d",&a);
            add_edge(0,a,1,0);
        }
        for(i=1;i<=k;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,inf,c);
            add_edge(b,a,inf,c);
        }
        for(i=1;i<=p;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(b,a+m,1,c);
        }
        int res=Min_Cost_Flow(s,t);
        printf("%d\n",res);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值