poj1125 所有(最短路中最大边的)最小边 bellman

Stockbroker Grapevine

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 31172   Accepted: 17053

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

Source

你的任务将是写一个程序,告诉您选择哪一个股票经纪人作为谣言的出发点和所花费多少时间将谣言扩散到整个社会的股票经纪人。这一期限是衡量过去的人收到信息所需的时间。

输入

你的程序包含多组股票经纪人的输入数据。每组以股票经纪人的人数开始。接下来的几行是每个经纪人与其他人接触的一些信息,包括这些人都是谁,以及将讯息传达到他们所需的时间。每个经纪人与其他人接触信息的格式如下:开头的第一个数表示共有n个联系人,接下来就有n对整数。每对整数列出的第一个数字指的是一个联系人(例如,一个‘1‘是指编号1的人),其次是在传递一个信息给那个人时所采取分钟的时间。没有特殊的标点符号或空格规则。

每个人的编号为1至经纪人数目。所花费的传递时间是从1到10分钟(含10分种)。股票经纪的人数范围是从1到100。当输入股票经纪人的人数为0时,程序终止。

输出

在对于每一组数据,你的程序必须输出一行,包括的信息有传输速度最快的人,以及在最后一个人收到消息后,所总共使用的时间(整数分钟计算)。

你的程序可能会收到的一些关系会排除一些人,也就是有些人可能无法访问。如果你的程序检测到这样一个破碎的网络,只需输出消息“disjoint”。请注意,所花费的时间是从A传递消息到B,B传递信息到A不一定是花费同样的传递时间,但此类传播也是可能的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 10000000

using namespace std;

struct node
{
	int u,v,w;
}edge[2000];
int n,num;
int low[2000];
int path[2000];
int Bellman(int u0)
{
	for(int i=0;i<=n;i++)
		low[i]=INF;
	low[u0]=0;
	for(int i=0;i<n-1;i++)
	{
		int flag=0;
		for(int j=0;j<num;j++)
		{
		if(low[edge[j].u]!=INF && edge[j].w+low[edge[j].u]<low[edge[j].v])
			{low[edge[j].v]=edge[j].w+low[edge[j].u];
			  flag=1;
			}
		}
		if(flag==0)
			break;
	}
	int Max=-1;
	for(int i=1;i<=n;i++)
	if(Max<low[i])
	Max=low[i];
	return Max;
}
int main()
{
	while(~scanf("%d",&n))
	{
		if(n==0)
			break;
		num=0;
		for(int i=1;i<=n;i++)  //从1开始
		{
			int m;
			scanf("%d",&m);
			for(int j=1;j<=m;j++)
			{
				int x,t;
				scanf("%d%d",&x,&t);
				edge[num].u=i;
				edge[num].v=x;
				edge[num++].w=t;
			}
		}

		int Min=INF,biao=-1;
		for(int i=1;i<=n;i++)
		{
			int k=Bellman(i);
			if(Min>k)
			{Min=k;
			 biao=i;
			}
		}
		if(biao==-1)
			printf("disjoint\n");
		else
			printf("%d %d\n",biao,Min);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-02-01 13:45:16

poj1125 所有(最短路中最大边的)最小边 bellman的相关文章

在传统以太网中,为什么要有最小帧长度和最大帧长度的限制

在传统以太网中,为什么要有最小帧长度和最大帧长度的限制? 以太网(IEEE 802.3)帧格式: 1.前导码:7字节0x55,一串1.0间隔,用于信号同步 2.帧起始定界符:1字节0xD5(10101011),表示一帧开始 3.DA(目的MAC):6字节 4.SA(源MAC):6字节 5.类型/长度:2字节,0-1500保留为长度域值,1536-65535保留为类型域值(0x0600-0xFFFF) 6.数据:46-1500字节 7.帧校验序列(FCS):4字节,使用CRC计算从目的MAC到数据

[算法]数组中未出现的最小正整数

题目: 给定一个无序整型数组arr,找到数组中未出现的最小正整数. 例如: arr=[-1,2,3,4].返回1. arr=[1,2,3,4].返回5. 要求时间复杂度为O(N),空间复杂度为O(1). 解答: 在遍历arr之前先生成两个变量.变量l表示遍历到目前为止,数组arr已经包含的正整数范围是[1,l],所以在没有开始之前l=0,表示arr没有包含任何正整数.变量r表示遍历到目前为止,在后续出现最优状况的情况下,arr可能包含的正整数范围是[1,r],所以在没有开始之前,令r=N,r同时

hdu1598find the most comfortable road(并查集+枚举,求起点到终点的边中最大边减最小边差值最小)

Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的"舒适度"有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ), 但XX星人对时间却没那么多要求.要你找出一条城市间的最舒适的路径.(

[算法]在数组中找到一个局部最小的位置

题目: 定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]为局部最小.如果arr[N-1]<arr[N-2],那么arr[N-1]是局部最小.如果0<i<N-1,arr[i]<arr[i+1]&&arr[i]<arr[i-1],那么arr[i]是局部最小. 给定无序数组arr,已知arr中任何两个相邻的数不相等.写一个函数,只需返回arr中任何一个局部最小出现

LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 求二叉树中第k个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊

算法总结之 数组中未出现的最小正整数

给定一个无序整型数组arr,找到数组中未出现的最小正整数 解题思路非常好,需要好好学习一下,很逻辑 如果arr长度为N, 最优解可以做到时间复杂度O(N) 额外空间复杂度O(1) 1.遍历arr之前生成两个变量, l  r   初始值 l=0   r=N 2.从左到右遍历arr,arr[l] 3.如果arr[l]=l+1 没有遍历arr[l]之前,arr已经包含的正整数范围是[1,l],此时出现了arr[l]=l+1的情况,所以arr包含的正整数范围可以扩展到[1,l+1]  即令 l++ 4.

查找数组中未出现的最小正整数

请设计一个高效算法,查找数组中未出现的最小正整数. 给定一个整数数组A和数组的大小n,请返回数组中未出现的最小正整数.保证数组大小小于等于500. 测试样例: [-1,2,3,4],4 返回:1 class ArrayMex { public: int findArrayMex(vector<int> A, int n) { // write code here // write code here sort(A.begin(),A.end()); //排序 if(A[0] > 1) r

数组中未出现的最小正整数(算法)

1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int s[100]; 5 int main() 6 { 7 int l,r; 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 for(int i=0;i<n;i++) 12 scanf("%d",s+i); 13 l=0; 14 r=n; 15 while

求无序数组中未出现的最小正整数

给定一个无序整型数组arr,找到数组中未出现的最小正整数.要求时间复杂度为O(N),空间复杂度为常数级. 例如: arr=[33,33,33,33]  返回1 arr=[34,56,45,12]  返回1 arr=[-1,2,3,4]  返回1 arr=[1,2,3,4]   返回5 ... 随便什么都行 原理很简单,代码用数据跑一遍就看出思想了. 1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 i