poj 1125 Stockbroker Grapevine dijkstra算法实现最短路径
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 23760 | Accepted: 13050 | 
Description
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
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
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
题目大意是信息必须通过关系网传播,信息在人与人之间传播需要一定的时间,现在给定一个关系网络,让我们求从哪个点开始传播才能使传播给所有人所有的时间最短,需要注意的是,信息的传播可以同时进行。
这题使用的dijkstra算法的优先级队列实现方式,
#include<stdio.h>
#include<queue>
#include<utility>
#include<string.h>
#define N 110
using namespace std;
int map[N][N];
int m;
int dij(int i)
{
int count = 0;//¼Ç¼ËÑË÷¹ýµÄµã£¬ÓÃÀ´ÅжÏÊÇ·ñÁªÍ¨
int ans = 0;
priority_queue<pair<int, int> , vector<pair<int, int> > , greater<pair<int, int > > > pq;
bool used[N] = {0};
pair<int ,int > pa;
pa.first = 0;
pa.second = i;
pq.push(pa);
while(!pq.empty())
{
pa = pq.top();
pq.pop();
if(used[pa.second])
continue;
used[pa.second] = 1;
count ++;
ans = ans > pa.first ? ans : pa.first;
if(count == m)
break;
int j;
for(j = 1; j <= m; j++)
{
if(map[pa.second][j] && !used[j])
{
pair<int, int> new_p;
new_p.first = pa.first + map[pa.second][j];
new_p.second = j;
pq.push(new_p);
}
}
}
if(count == m)
return ans;
else
return 0x7fffffff;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n;
while(scanf("%d", &m), m)
{
memset(map, 0, sizeof(map));
int j;
int i;
int min = 0x7fffffff, mark;
for(j = 1; j <= m; j++)
{
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
int to, time;
scanf("%d%d",&to, &time);
map[j][to] = time;
}
}
for(i = 1; i <= m; i++)
{
int temp = dij(i);
if(temp < min)
{
min = temp;
mark = i;
}
}
if(min == 0x7fffffff)
printf("disjoint\n");
else
printf("%d %d\n", mark, min);
}
return 0;
}
poj 1125 Stockbroker Grapevine dijkstra算法实现最短路径的更多相关文章
- Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)
		一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ... 
- 最短路(Floyd_Warshall) POJ 1125  Stockbroker Grapevine
		题目传送门 /* 最短路:Floyd模板题 主要是两点最短的距离和起始位置 http://blog.csdn.net/y990041769/article/details/37955253 */ #i ... 
- OpenJudge/Poj 1125 Stockbroker Grapevine
		1.链接地址: http://poj.org/problem?id=1125 http://bailian.openjudge.cn/practice/1125 2.题目: Stockbroker G ... 
- POJ 1125 Stockbroker Grapevine【floyd简单应用】
		链接: http://poj.org/problem?id=1125 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ... 
- POJ 1125 Stockbroker Grapevine
		Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33141 Accepted: ... 
- poj 1125 Stockbroker Grapevine(多源最短)
		id=1125">链接:poj 1125 题意:输入n个经纪人,以及他们之间传播谣言所需的时间, 问从哪个人開始传播使得全部人知道所需时间最少.这个最少时间是多少 分析:由于谣言传播是 ... 
- poj 1125   Stockbroker Grapevine(最短路径)
		Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a me ... 
- POJ 1125 Stockbroker Grapevine(最短路基础题)
		Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spre ... 
- POJ 1125 Stockbroker Grapevine 最短路 难度:0
		http://poj.org/problem?id=1125 #include <iostream> #include <cstring> using namespace st ... 
随机推荐
- 【转】 C#程序以管理员权限运行
			C#程序以管理员权限运行在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无 ... 
- javascript模块化编程(AMD规范的加载器)
			关于AMD规范可以参考阮一峰的这篇文章Javascript模块化编程(二):AMD规范 简单来说,AMD规范就是异步方式加载模块的一种方式,避免因为模块加载过慢而导致浏览器“假死”. 先贴一个学习地址 ... 
- Win7系统上配置使用Intellij Idea 13的SVN插件
			Win7系统上配置使用Intellij Idea 13的SVN插件 http://blog.csdn.net/jeepxiaozi/article/details/39856081 
- js 二维数组定义
			1.二维数组声明方式是下面这样的: var images=new Array(); //先声明一维 for(var i=0;i<10;i++){ //一维长度为10 images[i]=new ... 
- Android ImageButton android:scaleType
			ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType). android:scaleType是控制图片如 ... 
- [java] JNLP文件安装
			JNLP(Java Network Launching Protocol )是java提供的一种可以通过浏览器直接执行java应用程序的途径,它使你可以直接通过一个网页上的url连接打开一个java应 ... 
- JSP MVC
			Java的MVC玩起来比.Net的有意思,因为每一步你都知道它是如何运作的,都由自己去实现.而在.Net的MVC中,你却不知道一个Controller是如何对应到View中的,因为MS给了你IDE上的 ... 
- 剑指offer系列33-----把二叉树打印成多行
			[题目]从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 方法一:直接打印 package com.exe7.offer; import java.util.LinkedList; i ... 
- IntelliJ IDEA中怎么查看方法说明?
			View→Quick Documentation 查看当前配置的快捷键(例如Ctrl + Q) 在光标所在的方法上按下快捷键就可以看到方法的说明 下图为在View菜单中查看当前配置的快捷键截图: 下图 ... 
- java中四种引用类型(转)
			今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在网上查资料,感觉收获颇多, ... 
