WuKong 最短路&&记忆化搜索

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
***************************************************************************************************************************重点记忆化搜索
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<cmath>
  6 #define inf 0x3fffffff
  7 using namespace std;
  8 int map[330][330];
  9 int dis1[330],dis2[330];
 10 int dp[330][330];
 11 bool vis[330];
 12 int n;
 13 void dijstra(int u,int dist[])//求单源最短路径
 14 {
 15     memset(vis,false,sizeof(vis));
 16     int mins,i,j,v;
 17     for (i=1;i<=n;i++)
 18         dist[i]=map[u][i];
 19     dist[u]=0;
 20     vis[u]=true;
 21     while(1)
 22     {
 23         mins=inf;
 24         for (j=1; j<=n;j++)
 25             if (!vis[j]&&dist[j]<mins)
 26                 mins=dist[j],v=j;
 27         if (mins==inf)
 28             break;
 29         vis[v]=true;
 30         for (j=1;j<=n;j++)
 31             if (!vis[j]&&dist[v]+map[v][j]<dist[j])
 32                 dist[j]=dist[v]+map[v][j];
 33     }
 34 }
 35 
 36 
 37 int dfs(int a,int b)//记忆化搜索
 38 {
 39     int it,jt,kt;
 40     int v=0;
 41       if(dp[a][b]!=-1)
 42         return dp[a][b];
 43       if(a==b)//a,b终点相同时,各自向前走一步
 44       {
 45           v++;
 46           for(it=1;it<=n;it++)
 47           {
 48               if(dis1[it]+map[it][a]!=dis1[a])
 49                 continue;
 50               for(jt=1;jt<=n;jt++)
 51               {
 52                   if(dis2[jt]+map[jt][b]==dis2[b])
 53                     v=max(v,dfs(it,jt)+1);
 54               }
 55           }
 56       }
 57       for(it=1;it<=n;it++)//不相同时,甲向前走一小步
 58       {
 59           if(dis1[it]+map[it][a]==dis1[a])
 60            v=max(v,dfs(it,b));
 61       }
 62       for(it=1;it<=n;it++)//乙向前走一步
 63       {
 64           if(dis2[it]+map[it][b]==dis2[b])
 65            v=max(v,dfs(a,it));
 66       }
 67       dp[a][b]=v;//每次记录最大的结果
 68       return v;
 69 }
 70 void init()
 71 {
 72     int it,jt;
 73     for(it=1;it<=n;it++)
 74      for(jt=1;jt<=n;jt++)
 75        map[it][jt]=inf;
 76     memset(dp,-1,sizeof(dp));
 77 }
 78 
 79 int main()
 80 {
 81     int u,v,w;
 82     int a,b,c,d,m;
 83   while(scanf("%d %d",&n,&m),(n||m))
 84   {
 85       init();
 86       while(m--)
 87       {
 88           scanf("%d%d%d",&u,&v,&w);
 89           if(w<map[u][v])
 90             map[u][v]=map[v][u]=w;
 91       }
 92       scanf("%d%d%d%d",&a,&b,&c,&d);
 93       dp[a][c]=0;
 94       if(a==c)//这个很重要
 95        dp[a][c]=1;
 96       dijstra(a,dis1);
 97       dijstra(c,dis2);
 98       printf("%d\n",dfs(b,d));
 99   }
100   return 0;
101 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3430134.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值