<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块后跟一个或多个 ...
随机推荐
- Gradle 编译多个project(包括多Library库project依赖)指导
Gradle Android最新自己主动化编译脚本教程(提供demo源代码) 这篇文章我简单写了基于Gradle2.1 进行的android project和android library的编译实例, ...
- 深搜解Riding the Fences
Riding the Fences Farmer John owns a large number of fences that must be repairedannually. He traver ...
- $(window).load(function(){})跟$(document).ready(function(){})跟$(function(){})区别
1.页面DOM加载完成 2.$(document).ready(function(){}) 的简写是 $(function(){}) 执行 3.图片样式等所有HTML元素加载完毕 4.$(windo ...
- Resolving Problems installing the Java JCE Unlimited Strength Jurisdiction Policy Files package--转
原文地址:https://www.ca.com/us/services-support/ca-support/ca-support-online/knowledge-base-articles.tec ...
- if switch
一.基本if结构: 1.语法:if (条件){ 代码块 } 2.执行顺序:先判断条件,条件成立则行{}内的代码,不成立则跳出if结构快既不执行{}内的代码. 3.什么情况下要用基本if选择结构:当需要 ...
- Windbg调试托管代码
Windbg调试.net托管代码需要借助于SOS.dll,.Net 4.0的32位sos.dll的路径在C:\Windows\Microsoft.NET\Framework\v4.0.30319,64 ...
- MyBatis数据持久化(八)sql复用
在mybatis中,我们可以將sql语句中公共的部分提取出来,然后需要该段sql的地方通过include标签引入即可,这样可以达到sql语句复用的目的. 例如我们有两条相似的查询语句: <sel ...
- 机器学习(十一) 支持向量机 SVM(上)
一.什么是支撑向量机SVM (Support Vector Machine) SVM(Support Vector Machine)指的是支持向量机,是常见的一种判别方法.在机器学习领域,是一个有监督 ...
- Caffe学习--Net分析
Caffe_Net 1.基本数据 vector<shared_ptr<Layer<Dtype> > > layers_; // 记录每一层的layer参数 vect ...
- ARC下dealloc过程及.cxx_destruct的探究
我是前言 这次探索源自于自己一直以来对ARC的一个疑问,在MRC时代,经常写下面的代码: - (void)dealloc { self.array = nil; self.string = nil; ...