<Sicily>Catch the thief
一、题目描述
A thief has robbed a bank in city 1 and he wants to go to city N. The police know that the thief will choose the shortest path to get to his destination. As there may be multiple shortest paths, the police want to calculate the number of all the cities that the thief may visit. (Including city 1 and city N)
二、输入
The input may contain several test cases.
Each test case starts with a line containing an integer N, indicating the number of cities.
The next N lines describe a N*N matrix A. the jth element in the ith row indicates the distance between city i and city j is Aij (Aij=Aji). Aij=-1 means city i and city j is not connected.
N=0 indicates the end of the input.
三、输出
For each test case, just print the total number of the cities the thief may visit in a single line.
例如:
输入:
5
0 1 4 -1 -1
1 0 -1 2 -1
4 -1 0 -1 2
-1 2 -1 0 3
-1 -1 2 3 0
0
输出:
5
四、解题思路
题意:
从起点0,到终点n,可能有多条最短路径,找出这些路径可能经过的节点。
这道题开始不知道怎么入手。有同学提醒说,求起点到各点的最短路径,终点到个点的最短路径,再取交点。
原理:起点0,终点n,假设点领到点n的最短路径长度为shortDis。判断是否有最短路径经过点1。点0到点1的最短路径为shortDisMatrix[0][1],点1到点n的最短路径为shortDisMatrix[1][n]。如果shortDisMatrix[0][1] + shortDisMatrix[1][n]==shortDis,那么有最短路径经过点1;同样使用这种方法判断其他点。
附:使用弗洛伊德算法求某点到其他各点的最短路径。
五、代码
#include<iostream>
using namespace std;
const int MAX_DIS = 500;
const int MAX_CITY_COUNT = 500;
int main()
{
int cityCount;
while(cin >> cityCount && cityCount > 0)
{
int disMatrix[MAX_CITY_COUNT][MAX_CITY_COUNT] = {0}; //保存个点距离
int distance;
for(int i = 0; i < cityCount; i++)
{
for(int j = 0; j < cityCount; j++)
{
cin >> distance;
if(distance < 0) {distance = MAX_DIS;}
disMatrix[i][j] = distance;
}
}
for(int i = 0; i < cityCount; i++) //使用Floyd算法求最短路劲
{
for(int j = 0; j < cityCount; j++)
{
for(int n = 0; n < cityCount; n++)
{
if(disMatrix[j][n] > disMatrix[j][i] + disMatrix[i][n])
disMatrix[j][n] = disMatrix[j][i] + disMatrix[i][n];
}
}
}
int result = 2;
for(int i = 1; i < cityCount - 1; i++) //判断i点是否有(起点到终点)最短路径经过
{
if((disMatrix[0][i] + disMatrix[i][cityCount - 1]) == disMatrix[0][cityCount - 1]) result++;
}
cout << result << endl;
}
return 0;
}
<Sicily>Catch the thief的更多相关文章
- 捉BUG记(To Catch a Bug)
大约有一年整没有写一篇博客了,由于各种原(jia)因(ban)导致闲暇时间要么拿着IPad看岛国奇怪的片(dong)子(hua).要么拿着kindle看各种各样的资(xiao)料(shuo).本来想写 ...
- HDU 3469 Catching the Thief (博弈 + DP递推)
Catching the Thief Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题
D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...
- csu 1356 Catch bfs(vector)
1356: Catch Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 96 Solved: 40[Submit][Status][Web Board] ...
- SQLServer如何添加try catch
在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...
- try...catch..finally
try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- [c#基础]关于try...catch最常见的笔试题
引言 在翻看之前总结的常见面试题中,关于try...catch异常处理的还是蛮多了,今天看到这个面试题,也就重新学习一下. try..catch语法 try-catch语句由一个try块后跟一个或多个 ...
随机推荐
- 阿里云部署Docker(8)----安装和使用redmine
安装redmine对过程进行管理. 须要说明的是:当你在docker images的时候,会说没连接到xxxx的时候,并且会提示用"docker -d".事实上这仅仅是把docke ...
- poj_1952最大下降子序列,统计个数
其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...
- zzulioj--1609--求和(数学规律)
1609: 求和 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 209 Solved: 67 SubmitStatusWeb Board De ...
- (转载)15 个 Android 通用流行框架大全
15 个 Android 通用流行框架大全 时间:2017-03-20 11:36来源:未知 作者:admin 点击: 2089 次 15 个 Android 通用流行框架大全 1. 缓存 Dis ...
- A. Amr and Music
解题思路:给出n种乐器学习所需要的时间,以及总共的天数, 问最多能够学多少门乐器,并且输出这几门乐器在原序列中的序号(不唯一) 按照升序排序,为了学到最多的乐器,肯定要选择花费时间最少的来学习 然后用 ...
- AJAX和JSON实际应用
实现功能:登录验证 一.因为我是在SpringMVC框架上写的,首先得添加依赖: <dependencies> <!-- 用来测试的依赖 --> <dependency& ...
- 第三方-FastDFS分布式文件系统
1.什么是FastDFS? FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制, 充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标, ...
- luogu P4139 上帝与集合的正确用法(扩展欧拉定理)
本蒟蒻现在才知带扩展欧拉定理. 对于任意的\(b\geq\varphi(p)\)有 \(a^b\equiv a^{b\ mod\ \varphi(p)+\varphi(p)}(mod\ p)\) 当\ ...
- POJ1743Musical Theme(后缀数组+二分)
题意 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需要满足如下条件: 1 ...
- Linux命令之bc - 浮点计算器、进制转换
用途说明 Bash内置了对整数四则运算的支持,但是并不支持浮点运算,而bc命令可以很方便的进行浮点运算,当然整数运算也不再话下.手册页上说bc是An arbitrary precision calcu ...