先上题目:

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. Why is try {…} finally {…} good; try {…} catch{} bad?

    http://stackoverflow.com/questions/128818/why-is-try-finally-good-try-catch-bad The big difference i ...

  2. LA4788

    贪心 这个贪心不太懂啊 dfs返回子树需要的最小值,然后按需要减消耗排序,然后贪心选取即可. #include<bits/stdc++.h> using namespace std; ty ...

  3. WingIDE4.1 破解及支持中文设置

    1.下面提供最新版本的破解方法. 先到http://wingware.com/downloads/wingide下载最新版本的IDE. 安装之前,先修改时间到一个月前. 安装 安装之后然后获取试用版的 ...

  4. javascript--给你的JS代码添加单元测试

    通过测试框架为JavaScript应用添加测试,从而保证代码的高质量.这里的笔记例子应用在jaywcjlove/validator.js中. 安装 用到三个工具chai(断言工具),mocha(测试框 ...

  5. 0522 json

    一.概念 json依赖于js和xml,是一种数据交换格式,json对比xml的生成和处理要更加方便.因此在许多领域,json正逐步取代xml的使用. 二.使用 1.在JS当中 json在javascr ...

  6. Oracle 12.2.0.1 RAC for rhel 7.X 数据库安装(节点1执行root.sh失败)

    说明: 最开始是用的rehat7.2安装12.2.0.1,后面安装GI节点一执行root.sh脚本失败,排查原因,最开始以为是操作系统的问题,换成rehat7.6,同样的出现问题,经过一番折腾,后面通 ...

  7. C - Stones on the Table

    Problem description There are n stones on the table in a row, each of them can be red, green or blue ...

  8. asp.net限制了上传文件大小为..M,解决方法

    asp.net限制了上传文件大小为4M,在:在web.config里加下面一句,加在<System.web></System.web>之间如下:<system.web&g ...

  9. Java实现九宫格

    import java.util.Scanner; public class Sudoku { public static void main(String[] args) { System.out. ...

  10. 1、Scala安装与基础

    1.scala与java 2.安装 3.scala编译器 4.变量声明 5.数据类型 6.操作符 7.函数调用 8.apply函数 1.scala与java scala基于java虚拟机,所有scal ...