HDU 2833 WuKong

WuKong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1351    Accepted Submission(s): 486


Problem Description
Liyuan wanted to rewrite the famous book “Journey to the West” (“Xi You Ji” in Chinese pinyin). In the original book, the Monkey King Sun Wukong was trapped by the Buddha for 500 years, then he was rescued by Tang Monk, and began his journey to the west. Liyuan thought it is too brutal for the monkey, so he changed the story:

One day, Wukong left his home - Mountain of Flower and Fruit, to the Dragon   King’s party, at the same time, Tang Monk left Baima Temple to the Lingyin Temple to deliver a lecture. They are both busy, so they will choose the shortest path. However, there may be several different shortest paths between two places. Now the Buddha wants them to encounter on the road. To increase the possibility of their meeting, the Buddha wants to arrange the two routes to make their common places as many as possible. Of course, the two routines should still be the shortest paths.

Unfortunately, the Buddha is not good at algorithm, so he ask you for help.
 

Input
There are several test cases in the input. The first line of each case contains the number of places N (1 <= N <= 300) and the number of roads M (1 <= M <= N*N), separated by a space. Then M lines follow, each of which contains three integers a b c, indicating there is a road between place a and b, whose length is c. Please note the roads are undirected. The last line contains four integers A B C D, separated by spaces, indicating the start and end points of Wukong, and the start and end points of Tang Monk respectively. 

The input are ended with N=M=0, which should not be processed.
 

Output
Output one line for each case, indicating the maximum common points of the two shortest paths.
 

Sample Input
  
  
6 6 1 2 1 2 3 1 3 4 1 4 5 1 1 5 2 4 6 3 1 6 2 4 0 0
 

Sample Output
  
  
3 Hint: One possible arrangement is (1-2-3-4-6) for Wukong and (2-3-4) for Tang Monk. The number of common points are 3.
 

Source
解题思路:给定一个无向图,从A到C的所有最短路和从B到D的所有最短路中选取两条,使得两条路径上的公共点最多,先跑SPFA,然后记忆化搜索,令dp[i][j]表示悟空以i为终点,唐僧以j为终点时,所拥有的公共点的最多的数目。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define Inf 999999999
using namespace std;
int head[305],k,dp[305][305],dis[2][305];
struct
{
    int e;
    int w;
    int next;
}edge[100005];
void add(int s,int e,int w)
{
    edge[k].e=e;
    edge[k].w=w;
    edge[k].next=head[s];
    head[s]=k++;
}
void spfa(int s,int n,int *dis)
{
    int i,st,ed;
    bool visit[305];
    memset(visit,false,sizeof(visit));
    queue<int> x;
    for(i=1;i<=n;i++)
        dis[i]=Inf;
    dis[s]=0;
    x.push(s);
    while(!x.empty())
    {
        st=x.front();
        x.pop();
        visit[st]=false;
        for(i=head[st];i!=-1;i=edge[i].next)
        {
            ed=edge[i].e;
            if(dis[ed]>dis[st]+edge[i].w)
            {
                dis[ed]=dis[st]+edge[i].w;
                if(!visit[ed])
                {
                    visit[ed]=true;
                    x.push(ed);
                }
            }
        }
    }
}
int dfs(int a,int b)
{
    if(dp[a][b]!=-1)
        return dp[a][b];
    int i,j,eda,edb,v=0;
    for(i=head[a];i!=-1;i=edge[i].next)
    {
        eda=edge[i].e;
        if(dis[0][eda]+edge[i].w==dis[0][a])
            v=max(v,dfs(eda,b));
    }
    for(j=head[b];j!=-1;j=edge[j].next)
    {
        edb=edge[j].e;
        if(dis[1][edb]+edge[j].w==dis[1][b])
            v=max(v,dfs(a,edb));
    }
    if(a==b)
        v++;
    dp[a][b]=v;
    return v;
}
int main()
{
    int i,m,n,a,b,c,d;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&m),m+n)
    {
        k=0;
        memset(head,-1,sizeof(head));
        memset(dp,-1,sizeof(dp));
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        scanf("%d%d%d%d",&a,&b,&c,&d);
        dp[a][c]=0;
        if(a==c)
            dp[a][c]=1;
        spfa(a,n,dis[0]);
        spfa(c,n,dis[1]);
        dfs(b,d);
        printf("%d\n",dp[b][d]);
    }
    return 0;
}


 
还有一种解法是跑floyd的同时用dp[i][j]表示i和j之间最大公共点的数目,然后如果dis[a][i]+dis[i][j]+dis[j][c]==dis[a][c],就表示dis[i][j]是a到c的最短路的一部分,同理b到d也一样,那么找出共同的dis[i][j],更新出dp[i][j]的最大值就是答案
#include<iostream>
const int N=444;
const int inf=10000000;
using namespace std;

int edge[N][N];
int path[N][N];//用path[i]][j]来表示i->j的路径上最多有多少个点
int n,m;

void floyd(){
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            if(i==k||edge[i][k]==inf)continue;
            for(int j=1;j<=n;j++){
                if(i==k||j==k||edge[k][j]==inf)continue;
                if(edge[i][k]+edge[k][j]<edge[i][j]){
                    edge[i][j]=edge[i][k]+edge[k][j];
                    path[i][j]=path[i][k]+path[k][j]-1;
                }else if(edge[i][k]+edge[k][j]==edge[i][j]&&path[i][j]<path[i][k]+path[k][j]){    //此时i,j,k为最短路径上的点
                    path[i][j]=path[i][k]+path[k][j]-1;
                }
            }
        }
    }
}

int solve(int v0,int u0,int v1,int u1){
    int ans=0;
    if(edge[v0][u0]>=inf||edge[v1][u1]>=inf)
        return 0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(edge[v0][i]+edge[i][j]+edge[j][u0]==edge[v0][u0]&&
                edge[v1][i]+edge[i][j]+edge[j][u1]==edge[v1][u1]){
                    ans=ans>path[i][j]?ans:path[i][j];
            }
        }
    }
    return ans;
}


int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++){
            edge[i][i]=0,path[i][i]=1;
            for(int j=i+1;j<=n;j++){
                edge[i][j]=edge[j][i]=inf;
                path[i][j]=path[j][i]=2;
            }
        }
        int x,y,s;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&s);
            if(edge[x][y]>s){
                edge[x][y]=edge[y][x]=s;
            }
        }
        floyd();
        int v0,v1,u0,u1;
        scanf("%d%d%d%d",&v0,&u0,&v1,&u1);
        int ans=solve(v0,u0,v1,u1);
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值