一、题目描述

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的更多相关文章

  1. 捉BUG记(To Catch a Bug)

    大约有一年整没有写一篇博客了,由于各种原(jia)因(ban)导致闲暇时间要么拿着IPad看岛国奇怪的片(dong)子(hua).要么拿着kindle看各种各样的资(xiao)料(shuo).本来想写 ...

  2. HDU 3469 Catching the Thief (博弈 + DP递推)

    Catching the Thief Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. 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 ...

  4. csu 1356 Catch bfs(vector)

    1356: Catch Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 40[Submit][Status][Web Board] ...

  5. SQLServer如何添加try catch

    在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...

  6. try...catch..finally

    try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...

  7. C++异常处理:try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  8. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  9. [c#基础]关于try...catch最常见的笔试题

    引言 在翻看之前总结的常见面试题中,关于try...catch异常处理的还是蛮多了,今天看到这个面试题,也就重新学习一下. try..catch语法 try-catch语句由一个try块后跟一个或多个 ...

随机推荐

  1. FPGA视频拼接器的放大和缩小功能

    视频视频器能够把信号源放大和缩小. 对于我们的拼接器而言,它的架构这种: 信号源进入到拼接器中.先进入缩小模块.然后存进DDR中.然后从DDR中读出视频.进入到放大模块,最后依据屏幕的位置,输出到屏幕 ...

  2. servlet修改后无效,仍然还是修改之前的效果

    注意servlet的路径是否正确,默认是java resources文件夹中的src.当在webcontent->web-inf->classses->data中时,注意添加路径.否 ...

  3. ThinkPHP5.0框架开发--第2章 TP5.0架构

    ThinkPHP5.0框架开发--第2章 TP5.0架构 第2章 TP5.0架构 ================================================== 上次复习 1.如 ...

  4. FireEye APT检测——APT业务占比过重,缺乏其他安全系统的查杀和修复功能

    摘自:https://zhidao.baidu.com/question/1694626564301467468.html火眼,APT威胁下快速成长 FireEye的兴起开始于2012年,这时段正好迎 ...

  5. 关于Java集合的小抄--转

    原文地址:http://calvin1978.blogcn.com/articles/collection.html 在尽可能短的篇幅里,将所有集合与并发集合的特征.实现方式.性能捋一遍.适合所有&q ...

  6. sas与mysql连接方法

    2012年8月11日 sas 9.1.3 版本 与mysql 连接 测试,可以与数据库连接1  通过odbc 直接连通 pass through connect to odbc create tabl ...

  7. HTML的常用标签属性及使用时需注意的一些细节

    前言 本篇随笔的主要是复习一下常用的一些HTML(Hyper Text Markup Language)标签及其属性,并总结一些使用过程中需要注意的一些细节,本篇提及的常用标签主要有: iframe标 ...

  8. 【算法】Floyd-Warshall算法(任意两点间的最短路问题)(判断负圈)

    求解所有两点间的最短路问题叫做任意两点间的最短路问题. 可以用动态规划来解决, d[k][i][j] 表示只用前k个顶点和顶点i到顶点j的最短路径长度. 分两种情况讨论: 1.经过顶点k,  d[k] ...

  9. hiho 1617 - 方格取数 - dp

    题目链接 描述 给定一个NxN的方格矩阵,每个格子中都有一个整数Aij.小Hi和小Ho各自选择一条从左上角格子到右下角格子的路径,要求路径中每一步只能向右或向下移动,并且两条路径不能相交(除了左上右下 ...

  10. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...