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 ...
随机推荐
- IPv6协议介绍
IPv6是为了解决基于IPv4的TCP/IP协议簇遇到的问题而推出的下一代IP协议.由于IPv4中采用的编制方式使得可用的网络地址和主机地址的数目远低于理论数目,随着全球互联网的快速发展,现有的IPv ...
- JSON结构
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- 命名空间 和 class_exist() 问题
#namespace_test_1.php <?php namespace namespace_test_1; class test { const NAMESPACE = 'namespace ...
- Linux 本地yum源搭建和网络yum源搭建
一.本地yum源搭建 首先挂载上光盘 [root@www /]# mount /dev/cdrom /media/cdrom/ 系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@ ...
- Oracle中排序列中值相同引发的问题(译)
This queston came up on the Oracle newsgroup a few days ago: 这个问题在Oracle的新闻中心被提出了一段时间: I have a tabl ...
- Redis源码研究--redis.h
------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...
- 【PHP】phpcms html去除空白
// 文件路径:/phpcms/libs/classes/template_cache.class.php 42行 // 第四第五行是新增的 $content = $this->template ...
- DirectoryEntry配置IIS出现ADSI Error:未知错误(0x80005000)
目录 问题案例 原因分析 解决问题 总结 问题案例 DirectoryEntry配置IIS,在IIS6.0下运转正常,但IIS7.0下运转会出错: System.DirectoryServices.D ...
- PAT IO-03 整数均值
/* *PAT IO-02 整数四则运算 *2015.7.30 *作者:flx413 */ #include<stdio.h> int main() { ], sum; float ave ...
- Java从入门到精通——数据库篇之JAVA中的对Oracle数据库操作
在Java中对Oracle数据库的操作分为两种:一.查询.二.非查询. 下面是我对其进行总结: 一.查询数据 /** * 根据用户代码查询 * @param userId * @return 如果存在 ...