先上题目:

WuKong

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

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.
 
  题意:给你一个无向图,给出孙悟空的出发地点和目的地点,唐僧的出发地点和目的地地点,问你如果他俩都走最短路的情况下(如果某个人有多条最短路的时候,那么这个人会走相遇点最多的那条),最多可以有多少个相遇的地方。
  这一题首先需要先求出两者的最短路,因为这里的点不是很多只有300个,所以可以用Flyod先求出多源最短路,同时需要求出某两点之间的最短路最多有多少个点。为什么需要求某两点最多有多少个点?这里经过分析可以得出,如果两条最短路有相交的部分,那么这些相交的部分一定是连续的。为什么呢?这里的分析和dij的分析一样。因为求最短路的时候对于某一个点延伸出去的时候是选最短的那条路,所以如果该点是重合点,那么如果还有重合点,那就意味着有一段最短路同时存在于两条最短路之间,所以如果我们求最短路的时候顺便把两点之间最短路最多经过了多少个点,那么只要我们枚举两条最短路中间的那一段就可以找到目标的最大值。
  求某两点的最短路最多有多少段的状态转移方程:
  dp[i][j]= max(dp[i][j],dp[i][u]+ dp[u][j])       dis[i][u]+dis[u][j]<dis[i][j]
        dp[i][j]                                          other
  在下面的程序里面dp[i][j]的意思是以i、j为端点的最短路最多有多少条边,所以结果要加一。
  枚举的时候:(dis[s1][i] + dis[i][j] + dis[j][e1] == dis[s1][e1]) && (dis[s2][i] + dis[i][j] + dis[j][e2] == dis[s2][e2])
  枚举的含义是dis[i][j]是s1e1的一段,同时也是s2e2的一段。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#define max(x,y) (x > y ? x : y)
#define MAX 302
#define INF 1000000000
using namespace std; int dis[MAX][MAX],dp[MAX][MAX];
int n,m; void flyod(){
for(int u=;u<=n;u++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dis[i][j]>dis[i][u]+dis[u][j]){
dis[i][j]=dis[i][u]+dis[u][j];
dp[i][j]=dp[i][u]+dp[u][j];
}else if(dis[i][j]==dis[i][u]+dis[u][j]){
dp[i][j]=max(dp[i][u]+dp[u][j],dp[i][j]);
}
}
}
}
} int main()
{
int a,b,l;
int s1,e1,s2,e2;
int ans;
//freopen("data.txt","r",stdin);
while(scanf("%d %d",&n,&m),(n+m)){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
dis[i][j]= i==j ? : INF;
dp[i][j]=;
}
}
for(int i=;i<m;i++){
scanf("%d %d %d",&a,&b,&l);
if(dis[a][b]>l){
dis[a][b]=dis[b][a]=l;
dp[a][b]=dp[b][a]=;
}
} scanf("%d %d %d %d",&s1,&e1,&s2,&e2);
flyod();
ans=-;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dp[i][j]>ans && (dis[s1][e1] == dis[s1][i]+dis[i][j]+dis[j][e1])
&& (dis[s2][e2] == dis[s2][i]+dis[i][j]+dis[j][e2]) ){
ans=dp[i][j];
}
}
}
printf("%d\n",ans+);
}
return ;
}

2833

HDU - 2833 - WuKong的更多相关文章

  1. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  2. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  3. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  4. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  5. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  6. 最短路&查分约束

    [HDU] 1548 A strange lift 根蒂根基最短路(或bfs)★ 2544 最短路 根蒂根基最短路★ 3790 最短路径题目 根蒂根基最短路★ 2066 一小我的观光 根蒂根基最短路( ...

  7. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

  8. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. VC UI界面库大集合

    Guitoolkit http://www.beyondata.com/pwc.html The Ultimate Toolbox http://www.codeproject.com/KB/MFC/ ...

  2. 安卓Recovery模式该怎么用?【转】

    本文转载自:http://android.baike.com/article-109914.html 安卓系统出了名的刷机刷机再刷机,说起刷机就不能不谈Recovery模式,这项刷机过程中最重要的一到 ...

  3. hdu4738Caocao's Bridges

    什么?有人要炸我的桥?!D飞他(心疼周瑜大都督) 这个就是求割边/桥了. #include<cstdio> #include<iostream> #include<cst ...

  4. Socket之shutdown()用法

    通常来说,socket是双向的,即数据是双向通信的.但有些时候,你会想在socket上实现单向的socket,即数据往一个方向传输. 单向的socket便称为半开放Socket.要实现半开放式,需要用 ...

  5. B3403 [Usaco2009 Open]Cow Line 直线上的牛 deque

    deque真的秀,queue和stack...没啥用了啊.操作差不多,就是在前面加一个front||back_就行了. 题干: 题目描述 题目描述     约翰的N只奶牛(编为1到N号)正在直线上排队 ...

  6. DCloud-MUI:事件管理

    ylbtech-DCloud-MUI:事件管理 极简的JS函数 1.返回顶部 1.事件绑定 除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on()方法实 ...

  7. yii引入js文件

    作者:zccst 四.在视图层(../views/..)添加CSS文件或JavaScript文件 Yii::app()->clientScript->registerScriptFile( ...

  8. LeetCode Weekly Contest 28

    1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...

  9. 打开手机摄像头扫描二维码或条形码全部操作(代码写的不好,请提出指教,共同进步,我只是一个Android的小白)

    (1)下载二维码的库源码 链接:http://pan.baidu.com/s/1pKQyw2n 密码:r5bv 下载完成后打开可以看到 libzxing 的文件夹,最后添加进 Android  Stu ...

  10. I2C controller core之Bit controller(05)

    6 generate statemachine 1 -- port cmd_ack : out std_logic; -- command completed 4 -- architecture ty ...