POJ 1125 Stockbroker Grapevine(floyd)
http://poj.org/problem?id=1125
题意 : 就是说想要在股票经纪人中传播谣言,先告诉一个人,然后让他传播给其他所有的经纪人,需要输出的是从谁开始传播需要的时间最短,输出这个人的编号和传播需要的最短的时间;
思路 : 典型的最短路问题,就是求最短传播时间,用floyd求出两两最短路。一般来说,因为告诉一个经纪人之后,他可以同时给其他他可以传播的所有人进行传播,所以,只要找到他需要传播的许多人中,找那个传播时间最长的,就能保证他可以给所有他能传播的人都传播到这个谣言,而其余他不能传播到的人,就要靠已传播的人继续往下传播。找n个人中以每个人为开始点的时候传播到所有人中的最长的那个时间以确保所有人都收到谣言,而再循环遍历一下,找这n个最长时间中最短的那个就可以了。
样例解释 : 3
2 2 4 3 5
2 1 2 3 6
2 1 2 2 2
5
3 4 4 2 8 5 3
1 5 8
4 1 6 4 10 2 7 5 2
0
2 2 5 1 5
0
样例是多组输入的,第一行输入n,以0结束,下面是n行,编号默认由1到n代表着n个经纪人,每一行开头一个数字m,代表着编号为i的经纪人可以与m个人传递谣言,然后就是m组数,每组有两个数字,一个是可以与编号为x的经纪人传递消息,另一个是传递消息需要的时间
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int oo = <<;
const int maxn = ;
int map[maxn][maxn];
int n; void floyd()
{
for(int k = ; k <= n ; k ++)
{
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= n ; j++)
{
if( map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k]+map[k][j];
}
}
}
} void Init()
{
for(int i = ; i <= n ; i ++)
{
for(int j = ; j <= n ; j++)
{
map[j][i] = map[i][j] = oo;
}
}
} int main()
{
while(~scanf("%d",&n)&&n)
{
Init();
int m,x,y;
for(int i = ; i <= n ; i++)
{
scanf("%d",&m);
for(int j = ; j <= m ; j++)
{
scanf("%d %d",&x,&y);
map[i][x] = y ;
}
}
floyd();
int maxlength;
int min_in_max = oo ;
int flag_source;
for(int i = ; i <= n ; i++) //以i点作为各通路源点
{
maxlength=;
for(int j = ; j <= n ; j++)
if(i != j && maxlength < map[i][j]) //寻找i到j的最长路径
maxlength = map[i][j];
if(min_in_max > maxlength)
{
min_in_max = maxlength; //寻找最长路径中的最短路
flag_source = i ; //该短路所在路径的源点记录
}
}
if(min_in_max<oo)
cout<<flag_source<<' '<<min_in_max<<endl;
else
cout<<"disjoint"<<endl;
}
return ;
}
POJ 1125 Stockbroker Grapevine(floyd)的更多相关文章
- poj 1125 Stockbroker Grapevine(多源最短)
id=1125">链接:poj 1125 题意:输入n个经纪人,以及他们之间传播谣言所需的时间, 问从哪个人開始传播使得全部人知道所需时间最少.这个最少时间是多少 分析:由于谣言传播是 ...
- POJ 1125 Stockbroker Grapevine【floyd简单应用】
链接: http://poj.org/problem?id=1125 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Stockbroker Grapevine(floyd)
http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...
- poj 1125 Stockbroker Grapevine(最短路 简单 floyd)
题目:http://poj.org/problem?id=1125 题意:给出一个社交网络,每个人有几个别人可以传播谣言,传播谣言需要时间.问要使得谣言传播的最快,应该从那个人开始传播谣言以及使得所有 ...
- POJ 1125 Stockbroker Grapevine(最短路基础题)
Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spre ...
- POJ Stockbroker Grapevine(floyd)
https://vjudge.net/problem/POJ-1125 题意: 题意不是很好理解,首先输入一个n,表示有n个股票经纪人,接下来输入n行,每行第一个数m为该股票经纪人认识的经纪人数,然后 ...
- poj 1125 Stockbroker Grapevine (dij优化 0ms)
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...
- POJ 1125 Stockbroker Grapevine【floyd】
很裸的floyd #include<cstdio> #include<string.h> #include<algorithm> #define maxn 201 ...
- 最短路(Floyd_Warshall) POJ 1125 Stockbroker Grapevine
题目传送门 /* 最短路:Floyd模板题 主要是两点最短的距离和起始位置 http://blog.csdn.net/y990041769/article/details/37955253 */ #i ...
随机推荐
- struts2中constant参数设置
序号 方法 说明 1 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定web应用默认 ...
- FPGA笔记-读取.dat文件
读取.dat图像文件 .dat文件是matlab生成的图像文件 initial begin // Initialize Inputs CLK = 0; RST = 1; IMAGE_DATA = 0; ...
- python 快速入门
根据以下几个步骤来快速了解一下python,目标是可以利用python来处理一些简易的问题或者写一些工具. 1.编写Hello world 2.学习 if,while,for 的语法 3.学习该语 ...
- MAC上 nodejs express 安装
最近在MAC上搭建 nodejs环境以及安装 express 框架,遇到了一些问题,不过最后总算还是安装成功了,下面是操作步骤 1.node js 安装 访问nodejs官网进入下载mac上的安装包 ...
- C#中泛型和单链表
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类 ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- Zendframework application 引导过程
Applications 会期望用户提供一个配置好的ServiceManager.提供以下服务: 1.EventManager 2.ModuleManager 3.Request 4.Response ...
- Java集合的小抄
在尽可能短的篇幅里,将所有集合与并发集合的特征.实现方式.性能捋一遍.适合所有"精通Java",其实还不那么自信的人阅读. [转自:花钱的年华] 期望能不止用于面试时,平时选择数据 ...
- VirtualBox中虚拟Ubuntu添加新的虚拟硬盘
VirtualBox中装好Ubuntu后,发现硬盘空间不够使用 了.以下是搜集整理的解决办法: 1. 添加新硬盘 设置 -> Storage -> SATA控制器->右击,选择&qu ...
- usb wifi driver run in ubuntu support 360/xiaodu and with 3.13.0-32-generic
(为了实现usb-wifi用在linux系统上,需求解决方案,过程记录和如何实现) 重点解决3.13.0-32-generic内核编译 mt7601 usb wifi 驱动问题. 1:首先下载MT76 ...