Risk
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2915   Accepted: 1352

Description

Risk is a board game in which several opposing players attempt to conquer the world. The gameboard consists of a world map broken up into hypothetical countries. During a player's turn, armies stationed in one country are only allowed to attack only countries with which they share a common border. Upon conquest of that country, the armies may move into the newly conquered country. 
During the course of play, a player often engages in a sequence of conquests with the goal of transferring a large mass of armies from some starting country to a destination country. Typically, one chooses the intervening countries so as to minimize the total number of countries that need to be conquered. Given a description of the gameboard with 20 countries each with between 1 and 19 connections to other countries, your task is to write a function that takes a starting country and a destination country and computes the minimum number of countries that must be conquered to reach the destination. You do not need to output the sequence of countries, just the number of countries to be conquered including the destination. For example, if starting and destination countries are neighbors, then your program should return one. 
The following connection diagram illustrates the first sample input. 

Input

Input to your program will consist of a series of country configuration test sets. Each test set will consist of a board description on lines 1 through 19. The representation avoids listing every national boundary twice by only listing the fact that country I borders country J when I < J. Thus, the Ith line, where I is less than 20, contains an integer X indicating how many "higher-numbered" countries share borders with country I, then X distinct integers J greater than I and not exceeding 20, each describing a boundary between countries I and J. Line 20 of the test set contains a single integer (1 <= N <= 100) indicating the number of country pairs that follow. The next N lines each contain exactly two integers (1 <= A,B <= 20; A!=B) indicating the starting and ending countries for a possible conquest. 
There can be multiple test sets in the input file; your program should continue reading and processing until reaching the end of file. There will be at least one path between any two given countries in every country configuration. 

Output

For each input set, your program should print the following message "Test Set #T" where T is the number of the test set starting with 1. The next NT lines each will contain the result for the corresponding test in the test set - that is, the minimum number of countries to conquer. The test result line should contain the start country code A the string " to " the destination country code B ; the string ": " and a single integer indicating the minimum number of moves required to traverse from country A to country B in the test set. Following all result lines of each input set, your program should print a single blank line.

Sample Input

1 3
2 3 4
3 4 5 6
1 6
1 7
2 12 13
1 8
2 9 10
1 11
1 11
2 12 17
1 14
2 14 15
2 15 16
1 16
1 19
2 18 19
1 20
1 20
5
1 20
2 9
19 5
18 19
16 20

Sample Output

Test Set #1
1 to 20: 7
2 to 9: 5
19 to 5: 6
18 to 19: 2
16 to 20: 2
题解:很水的一道题,就是个最简单的最短路;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
int mp[MAXN][MAXN],d[MAXN],vis[MAXN];
void dijkscra(int u){
mem(vis,);
mem(d,INF);
d[u]=;
while(true){
int k=-;
for(int i=;i<=;i++){
if(!vis[i])if(k==-||d[i]<d[k])k=i;
}
if(k==-)break;
vis[k]=;
for(int i=;i<=;i++)
d[i]=min(d[i],d[k]+mp[k][i]);
}
}
int main(){
int kase=;
while(){
mem(mp,INF);
int x,b,a;
for(int i=;i<=;i++){
if(!~scanf("%d",&x))exit();
while(x--){
scanf("%d",&b);
mp[i][b]=mp[b][i]=;
}
}
if(!~scanf("%d",&x))exit();
printf("Test Set #%d\n",++kase);
while(x--){
scanf("%d%d",&a,&b);
dijkscra(a);
printf("%d to %d: %d\n",a,b,d[b]);
}
puts("");
}
return ;
}

Risk(最短路)的更多相关文章

  1. UVa567_Risk(最短路)(小白书图论专题)

    解题报告 option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=508"& ...

  2. 任意两点之间的最短路(floyed)

    F.Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each ...

  3. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  4. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  5. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  6. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  7. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  8. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. 最短路(代码来源于kuangbin和百度)

    最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...

随机推荐

  1. linux脚本实例之while

    写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数 值,然后再次要求用户输入数值.直到用户输入"end"停 ...

  2. 【POJ 3009 Curling2.0 迷宫寻径 DFS】

    http://poj.org/problem?id=3009 模拟冰壶的移动,给出到达终点的最少投掷次数(不可达时为-1). 具体移动规则如下: 每次选四个方向之一,沿此方向一直前进,直到撞到bloc ...

  3. mac 上配置sublime text3插件

    1.安装MAC 版 sublime text 3 安装插件管理器 打开Sublime,按下Control + `(Mac)或者Ctrl + `(Windows),然后粘贴上下面的代码: import ...

  4. Unity 之 Redux 模式(第二篇)—— Rigidbody 改造,摄像机控制

    作者:软件猫 日期:2016年12月8日 转载请注明出处:http://www.cnblogs.com/softcat/p/6144041.html 上一篇文章中存在一个很严重的问题,首先我们先让 M ...

  5. hdu 1978 How many ways(dp)

    Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标 ...

  6. Duanxx的STM32学习:STM32下载方式选择

    前几天熟悉了STM32的启动方式.主要由Boot0和Boot1设置 如今须要解决的就是STM32的下载的问题. 一開始的时候,我选择的是SWD下载.这样的下载方式须要Boot0=0.Boot1=0.占 ...

  7. 安装Linux系统到u盘

    第一步:首先插入u盘到电脑主机usb接口处(建议插入到主机箱后置的usb接口).然后打开UltraISO软件,再打开选择须要写入u盘的Ubuntu 10.04或者其它版本号的Linux系统的iso镜像 ...

  8. Mansory算法分析

    相信大家对mansory排版算法印象十分深刻,它能够十分有效的实现页面紧凑排版,节省空间,并且还显得十分美观.在很多网站,包括鼎鼎有名的pinterest都使用了这个算法来实现排版.这个过程有点象瓦匠 ...

  9. C# Winform下载文件并显示进度条

    private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...

  10. VS2012JSON自动生成对应的类

    一.复制JSON数据如图 {Key:"aaaa",Value:"bbbb"} 二.点击以下操作