poj1603 floyd算法入门

POJ2240-Arbitrage【Floyd】 北大POJ2240-Arbitrage【Floyd】 解题报告+AC代码 立即下载

 

Risk is a board game in which several opposing playersattempt to conquer the world. The gameboard consists of a world map broken upinto hypothetical countries. During a player's turn, armies stationed in onecountry are only allowed to attack only countries with which they share acommon border. Upon conquest of that country, the armies may move into thenewly conquered country.

During the course of play, a player often engages in a sequence of conquestswith the goal of transferring a large mass of armies from some starting countryto a destination country. Typically, one chooses the intervening countries soas to minimize the total number of countries that need to be conquered. Given adescription of the gameboard with 20 countries each with between 1 and 19connections to other countries, your task is to write a function that takes astarting country and a destination country and computes the minimum number ofcountries that must be conquered to reach the destination. You do not need tooutput the sequence of countries, just the number of countries to be conqueredincluding the destination. For example, if starting and destination countriesare 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 countryconfiguration test sets. Each test set will consist of a board description onlines 1 through 19. The representation avoids listing every national boundarytwice 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 indicatinghow many "higher-numbered" countries share borders with country I,then X distinct integers J greater than I and not exceeding 20, each describinga boundary between countries I and J. Line 20 of the test set contains a singleinteger (1 <= N <= 100) indicating the number of country pairs thatfollow. 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 continuereading and processing until reaching the end of file. There will be at leastone path between any two given countries in every country configuration.

Output

For each input set, your program should print thefollowing message "Test Set #T" where T is the number of the test setstarting with 1. The next NT lines each will contain the result for thecorresponding test in the test set - that is, the minimum number of countriesto conquer. The test result line should contain the start country code A thestring " to " the destination country code B ; the string ":" and a single integer indicating the minimum number of moves required totraverse from country A to country B in the test set. Following all result linesof 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

 

 

 

 

题目分析:题意给出国家间的邻接关系,求从一个国家到另一个国家所经过的最少国家数。构建图每条边权为1,然后用floyd算法即可。另也可以用bfs。

Floyd算法用于求最短路径。三个for循环就可以解决问题,所以它的时间复杂度为O(n^3)。

Floyd算法的基本思想如下:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B。所以,我们假设Dis(AB)为节点A到节点B的最短路径的距离,对于每一个节点X,我们检查Dis(AX) + Dis(XB) < Dis(AB)是否成立,如果成立,证明从A到X再到B的路径比A直接到B的路径短,我们便设置Dis(AB)= Dis(AX) + Dis(XB),这样一来,当我们遍历完所有节点X,Dis(AB)中记录的便是A到B的最短路径的距离。

 

 

 

AC代码:

#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;

int cost[25][25];         //图的权

int main()

{

   int n,m,ca=1;

   int a,t;

   while(~scanf("%d",&a)){

       memset(cost,11,sizeof(cost));

       for(int i=1;i<=a;++i){

           scanf("%d",&t);

           cost[1][t]=cost[t][1]=1;

       }

       for(int i=2;i<=19;++i){

           scanf("%d",&t);

           for(int j=1;j<=t;++j){

                int x;

                scanf("%d",&x);

                cost[i][x]=cost[x][i]=1;

           }

       }

       for(int k=1;k<=20;++k)  //floyd

           for(int i=1;i<=20;++i)

                for(int j=1;j<=20;++j)

                   cost[i][j]=min(cost[i][k]+cost[k][j],cost[i][j]);

       scanf("%d",&m);

       printf("Test Set #%d\n",ca++);

       for(int i=0;i<m;++i){

           int a,b;

           scanf("%d%d",&a,&b);

           printf("%d to %d: %d\n",a,b,cost[a][b]);

       }

       printf("\n");

    }

   return 0;

}

基于级联H桥的储能变流器仿真与性能优化 本文深入探讨了基于级联H桥的储能变流器,通过仿真建模与性能优化,详细解析了其模块化结构、载波移相SPWM控制策略及核心性能指标。重点攻克了电池SOC均衡难题,介绍了利用零序电压注入实现相内均衡及功率调节实现相间均衡的实战策略,为提升储能系统效率与电池寿命提供了关键技术路径。 阅读详情

相关推荐

二叉树的操作之统计二叉树中节点的个数

一,问题描述 给定一颗二叉树,已知其根结点。 ①计算二叉树所有结点的个数 ②计算二叉树中叶子结点的个数 ③计算二叉树中满节点(度为2)的个数   二,算法分析 找出各个问题的基准条件,然后采用递归的方式实现。 ①计算二叉树所有结点的个数 1)当树为空时,结点个数为0,否则为根节点个数 加上 根的左子树中节点个数 再加上 根的右子树中节点的个数 借助遍历二叉树的思路,每访问一个结...

weixin_34200628的博客 4572

POJ图论问题 ——Floyd算法汇集

学习Floyd算法中。。。。。。 Floyd : 计算任意两节点间的最短距离,三重循环,效率较低! POJ 1125 Stockbroker Grapevine http://poj.org/problem?id=1125 问题简述:一群股票经理人传递虚假消息,问从哪个人那里发出消息,可以使大家在最短的时间内都收到消息! 解法:先用floyd算法...

weixin_30408165的博客 215

用Verilog手把手教你搭建一个5级流水线的MIPS CPU(附完整代码与仿真)

本文详细介绍了如何使用Verilog从零构建一个5级流水线的MIPS处理器,包括开发环境配置、核心模块实现、流水线冲突处理等关键技术。通过完整的代码示例和仿真方法,帮助读者掌握CPU设计的基本原理和实践技巧,特别适合想要入门处理器设计的工程师和学习者。

weixin_30613727的博客 467

Frogger POJ - 2253 floyed最短路

Frogger POJ - 2253 floyed最短路

qq_63108111的博客 119

从零开始:用ADS仿真设计一个2.4GHz Wi-Fi低噪声放大器(含完整电路图)

本文提供了一份详细的ADS软件仿真指南,手把手教你从零开始设计一个2.4GHz Wi-Fi低噪声放大器。内容涵盖核心指标理解、晶体管选型与建模、匹配网络设计、偏置电路集成,并通过S参数仿真、噪声优化及线性度验证等步骤,最终呈现完整的优化电路图,助力射频初学者掌握LNA设计全流程。

weixin_29197835的博客 815

SAP S4业务伙伴(BP)实战指南:从角色分配到数据同步的完整流程

本文详细解析了SAP S4业务伙伴(BP)功能,从基础概念到实战操作,涵盖角色分配、数据同步及常见问题排查。通过BP机制,企业可高效管理客户、供应商等多重身份,实现数据共享和动态切换,显著提升主数据管理效率。特别适合SAP实施顾问和ERP管理员参考。

y2z3a4b5c的博客 879

POJ-2240 Arbitrage(floyd详解)

题目大意: 大概就是说,有n种货币,然后输入m种对换方式(单向),只要其中有任意一种货币在经过交换后回到源点能比原来的值还要大,那么就输出Yes,反之输出No 算法分析: 我一开始理解错了题意,以为是指定第一种货币经过交换后最后能比原来的值更大,当时直接拿dijkstra算法做的,后来反复WA。后来我去找博客,才发现,原来是任意一种货币增值就输出Yes。 理解题目后我开始用

crazymad的博客 507

软考「系统规划与管理师(第2版)」考后复盘|考试大纲深度解读

第2版大纲其实在定义一种新岗位:不是“会运维的人”,而是“能站在业务战略高度,规划信息系统、治理服务过程、驱动数字化转型的总设计师”。你读大纲,别只看到24章。基础篇给你世界观方法篇给你规划力能力篇给你管理力实践篇给你落地感格局决定分数,责任决定高度。先懂大纲要什么样的人,再决定自己怎么学。官方大纲以中国计算机技术职业资格网最新公布版本为准:https://www.ruankao.org.cn。

软件行业技术文化交流。 77

POJ Floyd入门

POJ1125 POJ2240 题: 题:

Since_natural_ran 702

48、量子系统中的信源编码

本文深入探讨了量子系统中的信源编码理论与应用,详细介绍了盲编码与可见编码、定长编码与变长编码四种主要信源编码方案。文章从经典信源编码出发,对比分析了量子场景下的压缩特性,并系统性地阐述了量子定长信源编码的构建方法及相关定理证明。同时,讨论了通用量子定长信源编码的设计思路及其在实际应用中的优势与挑战。通过理论分析与实验进展的结合,为量子信息存储、传输和处理提供了重要的理论基础和技术支持。

m2n3b4v5c6的博客 103

价值投资中的AI驱动现金流预测:多智能体协作模型

在价值投资领域,准确的现金流预测是做出明智投资决策的关键。传统的现金流预测方法往往依赖于历史数据和简单的统计模型,难以应对复杂多变的市场环境和企业经营状况。随着人工智能技术的快速发展,利用AI驱动的模型进行现金流预测成为了研究热点。本文的目的在于介绍一种基于多智能体协作的模型,用于提高价值投资中现金流预测的准确性和可靠性。本文的范围涵盖了多智能体协作模型的核心概念、算法原理、数学模型、项目实战、实际应用场景以及相关工具和资源推荐等方面,旨在为读者提供一个全面且深入的关于AI驱动现金流预测的知识体系。

AI 算法实战派 681

poj1603(Flody算法)

题意:题目确定只有20个国家,之间存在边界的话,距离设置为1。前面的19行,首先第i(1-19)行给出一个x,代表x后面跟的国家数,表示第i行和后面的国家之间存在边界,设置为1.第20行给出表示可能征服的起始国和结束国(也就是国家A和国家B之间的最短距离)。 使用Flody算法: 核心算法: for(int k=1;k<=20;k++){ for(int i=1;i<=20;i++){ for(int j=1;j<=20;j++){ e[i][j]=min(e[i][k

Keep_Trying_Go的博客 445

化工设备设计软件:PVELITE二次开发_(12).数据导入导出与兼容性

在数据导入之前,需要定义数据在PVELITE中的结构。可以通过编程接口定义字段名称和数据类型。# 导入必要的库 import pvelite # 创建PVELITE实例 pve = pvelite . PVELITE() # 定义CSV数据结构 csv_structure = {

kkchenjj的博客 1222

[floyd]POJ 1603 Risk

传送门:Risk Risk Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2502   Accepted: 1176 Description Risk is a board game in which several opposing players

jjaw2013 818

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

POJ1603 ZOJ1221 Risk【Floyd算法】

Risk Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3537 Accepted: 1580 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 countri

海岛Blog 649

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Floyd算法解决POJ 1603

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

weixin_30642029的博客 112

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

poj 1603 Risk(最短路floyd)

边权为 1 的最短路,floyd模板直接过。 有几个debug,错了几次。 代码

渐近自由 605

poj1603 Floyd最短路

也没啥好说的,纯粹给自己复习下算法,赤裸裸的Floyd最短路。 #include using namespace std; #define MAXVALUE 1<<29 int main() { const int nNumber=20; int graphMat[nNumber+1][nNumber+1]; int firstN; int nTest=0;

算法,时空数据挖掘 786

poj 1603 Floyd

//求所有节点之间的最短路 #include #include using namespace std; #define MAXN 21 int x,j; int n,a,b; int T; int dist[MAXN][MAXN]; void Floyd() { for(int k = 1; k <= 20; k++) { for(int i = 1; i

枯荣 775

【算法练习】图论/Floyd最短路 百练poj1603:Risk

题目链接:http://bailian.openjudge.cn/practice/1603 1603:Risk 总时间限制: 1000ms 内存限制: 65536kB 描述 Risk is a board game in which several opposing players attempt to conquer the world. The gameboard c...

一蓑烟雨任平生 233

POJ1603 floyd水题

神水之题。 但是最狗血的是我把输出:冒号用拼音输入形式的冒号。 wa了几千次。。。 突然想到上一次复杂bfs题目,wa到最后还是不知道为什么wa。 现在突然想起来那时候也是:用汉字形式的冒号。 坑爹。 #include using namespace std

Aszxqw 602

Poj 1603 Risk (Floyd)

简单的Floyd的运用,这里要注意的就是多组数据的输入,其他都没有什么难的了

K_Kelly_的博客 160

POJ 1603

<br />package poj; import java.util.Scanner; public class poj1603 { public static void main(String args[]){ Scanner in = new Scanner(System.in); int a[][]; int b[][]; int count=0; while(in.hasNext()){ //初始化邻接矩阵 count++; a

rommel1的专栏 875

poj 1603 floyd

题意:这题的输入是真你妹儿的坑爹啊~ 让我调这么久。。。就是各个城镇边权都是1,最后求a to b 的最短距离dist[a][b] 思路:Floyd  #include #define inf 1000000 using namespace std; int dist[23][23]; void floyd() { int i,j,k; for(k=1;k<=20;k++) fo

丿聪丶 366

poj 1603 Risk

flody的入门题,最普通的模板一套就A了,记住map每次都初始化就行。 #include #include #include using namespace std; int map[21][21]; int main(void) { int n; int v = 0; while(scanf("%d",&n)!=EOF) { for(int

purevegetable 886

POJ 1603 Risk 最短路 Floyd Dijstra

POJ 1603 每组数据先输入19组边信息,依次代表从第几个顶点出发有哪些边与之直接相连。 要注意的是图是无向图,所以在更改邻接矩阵时根据无向图的对称性来处理数据 就是求两点中间的最少的城市个数 public class Main { static int[][] w=new int[21][21]; static final int n=20; public static voi

qq_35109846的博客 362
上一篇: eoj1854 Playing With Cubes 二分图最大匹配
下一篇: poj1915 简单bfs
方品
博客等级 码龄14年 31粉丝 114原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值