问题描述

Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way.

Unfortunately for you, stockbrokers only trust information coming from their “Trusted sources” This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.

输入

Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a ‘1’ means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules.

Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people.

输出

For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes.
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message “disjoint”. Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.

样例

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个股票经纪人,每当有谣言时来每个股票经纪人都在某一个时刻传给他m个的联系人。
当现在来了一个谣言的时候,哪个股票经纪人最先知道这个谣言的时候,能最快地把这个谣言散步到大家都知道,最短时间是多少。
如果没有哪个股票经纪人得知谣言后能散布给所有人,则输出“disjoint”。

题解

用Floyd求出任意两点间的最短路。
然后枚举每个股票经纪人散布给其他人的最大的时间。
如果这个时间最大值为INF,说明哪个股票经纪人都无法把谣言散布给其他人,则输出“disjoint”。
否则就是谣言散布到所有人身上是最短的时间。

#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 1005;
const int maxn = 1e2+5;
int dp[maxn][maxn];
int n,m;
void Floyd()
{for(int k=1;k<=n;++k){for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){
//              if(i!=j)dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);}}}
}
int main()
{int v,e;while(cin>>n){if(n==0) break;for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){dp[i][j]=INF;}dp[i][i]=0; }for(int i=1;i<=n;++i){cin>>m;for(int j=0;j<m;++j){cin>>v>>e;dp[i][v]=e;            //顶点i到定点v之间有一条边,边权为e}}   Floyd();//      for(int i=1;i<=n;++i)
//      {
//          for(int j=1;j<=n;++j)
//          {
//              cout<<dp[i][j]<<" ";
//          }
//          cout<<endl;
//      }
//      cout<<endl;int res = INF;int pos = 0;for(int i=1;i<=n;++i){int max_e = -1;for(int j=1;j<=n;++j){if(i==j) continue;if( dp[i][j] > max_e){max_e = dp[i][j];}}if(max_e < res){res = max_e;pos = i;   }}if(res ==  INF){cout<<"disjoint"<<endl;}else {cout<<pos<<" "<<res<<endl;}}return 0;
}

poj1125 Stockbroker Grapevine Floyd算法相关推荐

  1. Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)

    一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...

  2. POJ-1125 Stockbroker Grapevine 最短路

    这题要处理的地方的就是一个人可以同时向多个人传递消息,也就是说一条消息的传递时间由最长的那一条路径所决定,因为可以同时进行嘛,所以就求某一点到所有点的最短路,然后再寻找一条最长的路劲,枚举每个顶点作为 ...

  3. Floyd算法C++实现与模板题应用

    简介 Floyd算法算是最简单的算法,没有之一. 其状态转移方程如下map[i , j] =min{ map[i , k] + map[k , j] , map[i , j] }: map[i , j ...

  4. 数据结构与算法(7-4)最短路径(迪杰斯特拉(Dijkstra)算法、弗洛伊德(Floyd)算法)

    目录 一.最短路径概念 二.迪杰斯特拉(Dijkstra)算法(单源最短路径) 1.原理 2.过程 3.代码 三.弗洛伊德(Floyd)算法(多源最短路径) 1.原理 2.存储 3.遍历 4.代码 参 ...

  5. 【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法)

    Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高.而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编 ...

  6. 最小环算法求解(Dijkstra算法+Floyd算法)

    方法一: #include<iostream> #include<algorithm> #include<cmath> #include<cstdio> ...

  7. HDU2544(Bellman-ford算法和Floyd算法)

    思路: 1.初始化时将起点 s 到各个顶点 v 的距离 dist(s->v) 赋值为 ∞,dist(s->s) 赋值为 0: 2.后续进⾏最多 n-1 次遍历操作 (n 为顶点个数), 对 ...

  8. 【图论专题】Floyd算法及其扩展应用

    Floyd的拓展应用: 任意两点最短路 传递闭包 找最小环 恰好经过k条边的最短路(倍增) 题目列表: 题目 算法 AcWing 1125. 牛的旅行 任意两点最短路Floyd AcWing 343. ...

  9. 【图论】用一道题从本质上讲清楚Floyd算法

    P1119 [灾后重建] 4 5 1 2 3 4 0 2 1 2 3 1 3 1 2 2 1 4 0 3 5 4 2 0 2 0 1 2 0 1 3 0 1 4 -1 -1 5 4 一道非常好的Flo ...

最新文章

  1. 数据库服务器 之 PostgreSQL数据库的日常维护工作
  2. 为什么parsefloat加出来还是字符串_为什么水质检测达标家里的自来水管流出来的水却还是脏的?...
  3. python最难学的是什么_python是最难学的语言吗
  4. Keepalived高可用集群来实现web服务器负载均衡集群
  5. 大学生html5设计大赛方案,2018年大学生三维设计大赛策划书范文
  6. spring boot application.properties 属性详解
  7. JavaScript高级程序设计(第三版)学习笔记1~5章
  8. eclipse linux windows 乱码,Ubuntu下Eclipse打开Windows下的工程文件乱码解决办法
  9. 找呀志_通过开源框架引AsyncHttpClient处理get/post要求
  10. Linux命令行下批量重命名文件名为数字索引编号(0~N.xxx)的方法
  11. 初涉IPC,了解AIDL的工作原理及用法
  12. Eclipse自己定义keystore
  13. 国科大计算机算法与分析——陈玉福 马菲菲
  14. 语音系统智能AI机器人AI源码营销机器人电销机器人智能电话机器人拨号机器人语音机器人空号识别FreeSWITCH呼叫中心中间ipbxIPBX科大识别阿里识别语音识别语音翻译
  15. matlab在循环中保存jpg格式_matlab中的图片保存方法
  16. 1-3年Android开发工程师面试经验分享,面试建议
  17. 在线教育平台项目——需求分析
  18. 2019年最新《Web 前端开发》等级考试模拟题~以国家 “1+X” 职业技能证书为标准,厚溥推出 Web 前端开发人才培养方案...
  19. sql server中对日期字段值的比较
  20. 编程语言排行,C# 和 C++ 有望超越C?

热门文章

  1. PhpSpreadsheet 实现Excel多sheet导出
  2. Android多点触控揭秘
  3. 带你领略3D转换的魅力~
  4. linux mysql搭建禅道详细教程_2019-08-28 redhat linux如何部署禅道服务器(一键安装包)...
  5. SAP物料清单MM60中如何统计输出条目数量
  6. ⼤数据平台基础架构及解决⽅案
  7. 数控木工机械和精美家具制作
  8. 红楼梦人物分析系统c语言,Gephi分析红楼梦
  9. 【网页制作】CSS文本和字体属性讲解【附讲解视频】
  10. mysql 1308_Mysql恢复数据报ERROR1308:LEAVEwithnomatchinglabel_MySQL