博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
acm algorithm practice 2010 Dec. 26
阅读量:5840 次
发布时间:2019-06-18

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

poj   1125     Stockbroker Grapevine

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.

---------------------------------------------------------------------------------------------

it's a basic dijkstra shortest path problem.

 

dijkstra algorithm :

choose a source vertex and set the its value 0

other vertices are set to infinite 

while(Q != empty){

  pick u = extract-min(Q)

  for each v in adj(u) && v still in Q{

    if(d(v) > d(u) + w(u,v)){

      d(v)= d(u)+w(u,v)

      parent(v) = u   //record

    }

  }

}

 

although it's not complicate, I still feel unfamiliar with the structure of graph. actually, the data structure I use in this problem is quite simple and not practical at all.   however, something in my implementation is still redundant :)......

代码
 
struct
edge {
int
from;
int
to;
//
vertex
int
edge_value;
};
struct
broker_info {
int
num;
int
path_value;
//
value of the vetex
map
<
int
, edge
>
adjacency;
//
all the adjecent vertex from this broker
};
map
<
int
, broker_info
>
broker1;
//
the information of this broker

 

another deficiency is that I didn't use heap or F tree store the graph,which makes the time complicity a little bit higher.    

the STL has a heap template:   make_heap()   etc.   

 

mistake : 

initialize,  recall that broker[i] will be changed in every loop of choosing new source vertex

 
for
(
int
i
=
1
; i
<=
b_num; i
++
) {
//
at beginning Q set = 1,2, ... b_num
Q.insert(i);
broker[i]
=
broker1[i];
}

 

 

转载于:https://www.cnblogs.com/ggppwx/archive/2010/12/27/1917670.html

你可能感兴趣的文章
一键安装Gitlab后的备份、迁移与恢复
查看>>
因为本人工作繁忙,精力有限,本博客停止更新。有兴趣的博友可以关注我在CSDN上的主博客...
查看>>
SQL server查看触发器是否被禁用
查看>>
[C++基础]在构造函数内部调用构造函数
查看>>
跟随我在oracle学习php(8)
查看>>
Spring 3.1.0 Hibernate 3.0 Eclipse Spring WEB例子
查看>>
UVA-10212 The Last Non-zero Digit. 分解质因子+容斥定理
查看>>
求两个集合的交集,并集,差集
查看>>
Kotlin的语法糖(一)基础篇
查看>>
OkHttp源码分析
查看>>
让你的app体验更丝滑的11种方法!冲击手机应用榜单Top3指日可待
查看>>
windows kernel exploitation基础教程
查看>>
NS_OPTIONS枚举的用法
查看>>
java9系列(九)Make G1 the Default Garbage Collector
查看>>
QAQ高精度模板笔记√
查看>>
Jmeter计数器的使用-转载
查看>>
【Android笔记】入门篇02:全屏设置和禁止横屏竖屏切换
查看>>
4. Median of Two Sorted Arrays
查看>>
Linux 虚拟机忘记root密码
查看>>
Kubernetes的本质
查看>>