zoukankan      html  css  js  c++  java
  • 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
    ",dfs(b,d));
     99   }
    100   return 0;
    101 }
    View Code
  • 相关阅读:
    7款强大的Javascript网格插件推荐 狼人:
    90后英国中学生建立黑客社交网 涉案金额达1.8亿元 狼人:
    好的代码里只要一个return语句 狼人:
    一个月内从零开始做webOS开发人员 狼人:
    FireFox 5开发计划曝光 内嵌PDF阅读器(组图) 狼人:
    谷歌用户体验设计准则 狼人:
    15个编程好习惯 狼人:
    Debain/ArchLinux/Gentoo 等将合并为超级Linux 狼人:
    别说你不知IE9正式版浏览器小技巧9则 狼人:
    Firebug1.8a1发布 新功能、新架构 狼人:
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3430134.html
Copyright © 2011-2022 走看看