HDU2448-spfa+KM

Mining Station on the Sea

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


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
 

Source
 


题目大意:

                  给你n个港口和m个采矿站位于海上,有n搜船开始位于采矿站的位置,给你k条边连接两个采矿站和花费的时间,p条边连接采矿站到港口和花费的时间,问你每搜船到达港口且每个港口只能听一艘船的最少花费


题目思路:

                 我们首先考虑将港口和采矿站建立二分图,然后就是个二分图带权问题,但是采矿站之间也能走,所以我们可以用spfa预处理每个采矿站到达每个港口的最小花费,然后就可以用KM求解最小花费,这里我们可以看成是n*n的图,只要考虑有船停靠的采矿站!


AC代码:

#include<cstring>
#include<cstdio>
#include<queue>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
using std::queue;
const int maxn = 1000;
const int inf= 1e8;
int n,m,k,p,e;
int hed[maxn],vis[maxn],dis[maxn],in[maxn];
queue<int>q;

struct nod{
    int v,w,nex;
}edge[maxn*maxn];

void spfa(int u){
    for(int i=1;i<=n+m;i++)dis[i]=inf;
    dis[u]=0,vis[u]=1;
    q.push(u);
    while(!q.empty()){
        u = q.front();q.pop();
        vis[u]=0;
        for(int i=hed[u];~i;i=edge[i].nex){
            int v = edge[i].v;
            if(dis[v]>dis[u]+edge[i].w){
                dis[v]=edge[i].w+dis[u];
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}

void init(){
    memset(hed,-1,sizeof(hed));
    memset(vis,0,sizeof(vis));
    e=1;
}

void add(int u,int v,int w){
    edge[e].v=v,edge[e].w=w,edge[e].nex=hed[u],hed[u]=e++;
}

int ex_x[maxn],ex_y[maxn],vis_x[maxn],vis_y[maxn],mp[maxn][maxn],link[maxn],slack[maxn];

void init1(){
    for(int i=1;i<=n+m;i++){
        ex_y[i]=0,ex_x[i]=-inf;
        link[i]=-1;
        for(int j=1;j<=n+m;j++){
            mp[i][j]=-inf;
        }
    }
}

bool dfs(int u){
    vis_x[u]=1;
    for(int i=m+1;i<=n+m;i++){
        if(!vis_y[i]&&mp[u][i]!=-inf){
            if(ex_x[u]+ex_y[i]==mp[u][i]){
                vis_y[i]=1;
                if(link[i]==-1||dfs(link[i])){
                    link[i]=u;
                    return true;
                }
            }else {
                slack[i]=min(slack[i],ex_x[u]+ex_y[i]-mp[u][i]);
            }
        }
    }
    return false;
}

void sove(){
    for(int i=0;i<n;i++){
        for(int j=m+1;j<=n+m;j++)slack[j]=inf;
        while(1){
            memset(vis_x,0,sizeof(vis_x));
            memset(vis_y,0,sizeof(vis_y));
            if(dfs(in[i]))break;
            int d = inf;
            for(int j=m+1;j<=n+m;j++)if(!vis_y[j])d=min(d,slack[j]);
            for(int j=1;j<=n+m;j++){
                if(vis_x[j])ex_x[j]-=d;
                if(vis_y[j])ex_y[j]+=d;
                else slack[j]-=d;
            }
        }
    }
    int res=0;
    for(int i=m+1;i<=n+m;i++){
        if(link[i]!=-1)res+=mp[link[i]][i];
    }
    printf("%d\n",-res);
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&k,&p))
    {
        init();
        init1();
        for(int i=0;i<n;i++)scanf("%d",&in[i]);
        while(k--){
            int u,v,w;scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);add(v,u,w);
        }
        while(p--){
            int u,v,w;scanf("%d%d%d",&u,&v,&w);
            add(v,u+m,w);
        }
        for(int i=0;i<n;i++){
            int u = in[i];
            spfa(u);                  //spfa预处理
            for(int j=m+1;j<=n+m;j++){ 
                mp[u][j]=max(mp[u][j],-dis[j]);       //建立二分图
                ex_x[u]=max(ex_x[u],mp[u][j]);
            }
        }
        sove();
    }

    return 0;
}




全部评论

相关推荐

老粉都知道小猪猪我很久没更新了,因为秋招非常非常不顺利,emo了三个月了,接下来说一下我的情况吧本人是双非本&nbsp;专业是完全不着计算机边的非科班,比较有优势的是有两段大厂实习,美团和字节。秋招面了50+场泡池子泡死的:滴滴&nbsp;快手&nbsp;去哪儿&nbsp;小鹏汽车&nbsp;不知名的一两个小厂其中字节13场&nbsp;两次3面挂&nbsp;两次2面挂&nbsp;一次一面挂其中有2场面试题没写出来,其他的都是全a,但该挂还是挂,第三次三面才面进去字节,秋招加暑期总共面了22次字节,在字节的面评可以出成书了快手面了8场,2次实习的,通过了但没去,一次2面挂&nbsp;最后一次到录用评估&nbsp;至今无消息滴滴三面完&nbsp;没几天挂了&nbsp;所有技术面找不出2个问题是我回答不上来的,三面还来说我去过字节,应该不会考虑滴滴吧,直接给我干傻了去哪儿一天速通&nbsp;至今无消息小鹏汽车hr&nbsp;至今无消息美团2面挂&nbsp;然后不捞我了,三个志愿全部结束,估计被卡学历了虾皮二面挂&nbsp;这个是我菜,面试官太牛逼了拼多多二面挂&nbsp;3道题也全写了&nbsp;也没问题是回答不出来的&nbsp;泡一周后挂腾讯面了5次&nbsp;一次2面挂&nbsp;三次一面挂,我宣布腾讯是世界上最难进的互联网公司然后还有一些零零散散的中小厂,但是数量比较少,约面大多数都是大厂。整体的战况非常惨烈,面试机会少,就算面过了也需要和各路神仙横向对比,很多次我都是那个被比下去的人,不过这也正常,毕竟谁会放着一个985的硕士不招,反而去招一个双非读化学的小子感觉现在互联网对学历的要求越来越高了,不仅仅要985还要硕士了,双非几乎没啥生存空间了,我感觉未来几年双非想要进大厂开发的难度应该直线上升了,唯一的打法还是从大二刷实习,然后苟个转正,不然要是去秋招大概率是炮灰。而且就我面字节这么多次,已经开始问很多ai的东西了,你一破本科生要是没实习没科研懂什么ai啊,纯纯白给了
不知名牛友_:爸爸
秋招你被哪家公司挂了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务