POJ 1125 Stockbroker Grapevine 弗洛伊德算法
发布日期:2021-06-29 14:37:39 浏览次数:2 分类:技术文章

本文共 4157 字,大约阅读时间需要 13 分钟。

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

3 2

3 10

分析

把每个经纪人当成顶点,经纪人传给其他经纪人当做有向边,时间数就是边权,环也就是经纪人到自己置为0,不通的置为inf,即无穷大(本题inf不用开太大,太大反而结果不一样),如果一个经纪人不能把消息传给剩余的其他经纪人,那么这个传递网络是破碎的,输出disjoint,否则我们只需寻找这个经纪人传递消息最长的那个时间(因为这个最长的时间就是该经纪人传给所有人所需要的时间),然后遍历所有的经纪人寻找最小的时间,即本题是求最长路径的最小值。

在这里插入图片描述

AC

#include
#include
#define mst(a,b) memset(a,b,sizeof(a))using namespace std;const int INF=0x3f3f3f3f;const int maxn=100+5;int n;int mp[maxn][maxn];void floyd(){
for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);}int main(){
ios::sync_with_stdio(false); cin.tie(0); while(cin>>n&&n) {
mst(mp,INF); for(int i=1;i<=n;i++) {
mp[i][i]=0; int m; cin>>m; while(m--) {
int j,time; cin>>j>>time; mp[i][j]=time; } } floyd(); int max_time=-INF; int min_time=INF; int index=0; for(int i=1;i<=n;i++) {
max_time=-INF; for(int j=1;j<=n;j++) {
max_time=max(max_time,mp[i][j]); } if(min_time>max_time) {
min_time=max_time; index=i; } } if(min_time!=INF) cout<
<<" "<
<

转载地址:https://chocolate.blog.csdn.net/article/details/98171759 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UVA-512 Spreadsheet Tracking【STL大法好】+(带你了解Vector相关方法+模板归纳)
下一篇:ZOJ 1041 Transmitters (凸包的简单应用)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月18日 14时07分13秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章