一、题目描述

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. leetcode题解||ZigZag Conversion问题

    problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...

  2. AspNet WebApi 中应用fo-dicom抛出异常:No codec registered for tranfer syntax:

    背景: 在做一个Dicom Web Service, 当中WADO-RS中须要解析TransferSyntax, 然后就用到了fo-dicom中的DicomFile.ChangeTransferSyn ...

  3. MainWndProc运行观察(有待研究)

    MainWndProc运行观察 把MainWndProc改写成如下代码,便于观察:procedure TWinControl.MainWndProc(var Message: TMessage);be ...

  4. 使用XMLHttpRequest解析json

    不适用内函数或者promise的方式,可以在外部提取到json数据 <!DOCTYPE html> <html lang="en"> <head> ...

  5. ThinkPHP5.0框架开发--第3章 TP5.0 配置

    ThinkPHP5.0框架开发--第3章 TP5.0 配置 第3章 TP5.0 配置 ========================================================= ...

  6. POJ 3188暴搜

    题意: 思路: 裸的暴搜 --. 但是要注意如果你不用所有的按键就能输出最优解的话一定要把所有的字母都安排到一个位置-. 我的一群PE就是这么来的-- 为什么写的人这么少-- // by Sirius ...

  7. 从SQL注入谈数据访问层

    什么是SQL注入? SQL注入就是应用程序的开发人员未预期的吧SQL语句传入到应用程序的过程,如果直接使用用户输入的值来构建SQL语句的应用程序是很可能会受到SQL注入攻击的.特别是基于浏览器的网络应 ...

  8. winforms控件

     我们在开发窗体应用时,控件是必不可少的今天我们就来认识一下控件 在认识控件之前还要先来认识一下窗体具体如下: 认识窗体和控件 窗体                                   ...

  9. HDU 1856 More is better【并查集】

    解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值 More is better Time Limit: 5000/1000 MS (J ...

  10. servlet中Session的用法

    ## (1)什么是Session? 服务器端为了保存用户的状态而创建的一个特殊的对象(即session对象).          当浏览器第一次访问服务器时,服务器会创建session对象(该    ...