Risk(最短路)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 2915 | Accepted: 1352 |
Description
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
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
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(最短路)的更多相关文章
- UVa567_Risk(最短路)(小白书图论专题)
解题报告 option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=508"& ...
- 任意两点之间的最短路(floyed)
F.Moving On Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn. Each ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- 最短路(Floyd)
关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...
- bzoj1266最短路+最小割
本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 最短路(代码来源于kuangbin和百度)
最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...
随机推荐
- 浅谈标签构建——TagBuilder
在很多项目中,可能我们需要写一些通用的控件标签,今天来简单的学习一下吧. 在前文中已经学习了 如何自定义MVC控件标签 ,感兴趣的朋友可以去看看. 今天主要还是讲解一下TagBuilder 我们打开源 ...
- UML_用例图
用例图主要用来描述"用户.需求.系统功能单元"之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包 ...
- hdu 4619 Warm up 2_最大独立集
三个人整个下午都想不出这题 后来看题解,竟然用匈牙利算法的最大独立集,我顿时晕了. 题意:给竖着和横着的方块,除去重叠的,最多能留下几个方块 #include <cstdlib> #inc ...
- java 基础知识
...是Java代码的一部分,表明可以接受任意数量的对象,例子如下: 感觉很有趣 public static double max(double... values){ double largest ...
- unity3d导出pdf
unity生成pdf格式,首先需要导入iTextSharp.dll ,下面是我写的一些方法,可以直接用.直接贴代码, using UnityEngine; using System.Collectio ...
- 优秀的目录文档内容查找,替换工具,可以飞快的帮助你查询大IIS日志哟。
这,是一款飞速的目录文档中内容查找的工具. 它,飞快精准的帮助你查询到你想搜索的文档中的内容. 它,是一款由非常牛B,我都不晓得姓名的作者开发的,冒失是C++的windows应用. 你,非常需要他. ...
- OC基础11:基本的C语言特性2
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 19.定义一个int类型的指针: int ...
- 蓝桥杯 BASIC 29 高精度加法(大数)
[思路]:大数处理都一样. [AC代码]:代码细节能够美化一下. #include <iostream> #include <algorithm> #include <c ...
- C++ STL源代码学习(map,set内部heap篇)
stl_heap.h ///STL中使用的是大顶堆 /// Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap ...
- iOS 键盘挡住UITextField
iOS经常使用的两个功能:点击屏幕和return隐藏虚拟键盘和解决虚拟键盘挡住UITextField的方法 iOS上面对键盘的处理非常不人性化,所以这些功能都须要自己来实现, 首先是点击return ...