poj1125 floyd

2024-04-28 17:18
文章标签 floyd poj1125

本文主要是介绍poj1125 floyd,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

如题:http://poj.org/problem?id=1125

 

Stockbroker Grapevine
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28384 Accepted: 15750

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

Southern African 2001

 

 

题目大意:求出所给有向图某一点到其他所有点所花费的时间中最大的那一个最小。

 

思路:floyd水过

 

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 105
#define INF 0x0fffffff

int n,m;
int map[MAXN][MAXN];

int main()
{
// freopen("C:\\1.txt","r",stdin);
 while(~scanf("%d",&n))
 {
  if(!n)
   break;
  int i,j,k;
  for(i=0;i<MAXN;i++)
   for(j=0;j<MAXN;j++)
    map[i][j]=INF;
  for(i=1;i<=n;i++)
   map[i][i]=0;
  for(i=1;i<=n;i++)
  {
   scanf("%d",&m);
   for(j=0;j<m;j++)
   {
    int v,cost;
    scanf("%d%d",&v,&cost);
    map[i][v]=cost;
   }
  }
  for(k=1;k<=n;k++)
   for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
     if(map[i][j]>map[i][k]+map[k][j])
      map[i][j]=map[i][k]+map[k][j];
  int d[MAXN]={0};
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    if(map[i][j]>d[i])
     d[i]=map[i][j];
  int v,min=INF;
  for(i=1;i<=n;i++)
   if(min>d[i])
   {
    min=d[i];
    v=i;
   }
  if(min==INF)
   printf("disjoint\n");
  else
   printf("%d %d\n",v,min);
 }
}

 

这篇关于poj1125 floyd的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/943803

相关文章

uva 10099(floyd变式)

题意: 有一个导游要带着一群旅客从一个城市到达另一个城市,每个城市之间有最大的旅客流量限制。 问最少几趟能将这些旅客从一个城市搞到另一个城市。 解析: 用floyd找出最小流量中的最大边,然后次数就是   ceil(总人数 / 最大承载量 - 1),-1的意思是导游每次也要在车上。 ps.老司机哭晕在厕所 代码: #include <iostream>#includ

uva 10048(floyd变式)

题意: 求两个点之间经过的路径中最大噪声最小的值。 解析: floyd的变式,每次取g[i][k] g[k][j]中的大边与当前边g[i][j]比较,取小。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#includ

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

hdu 4517 floyd+记忆化搜索

题意: 有n(100)个景点,m(1000)条路,时间限制为t(300),起点s,终点e。 访问每个景点需要时间cost_i,每个景点的访问价值为value_i。 点与点之间行走需要花费的时间为g[ i ] [ j ] 。注意点间可能有多条边。 走到一个点时可以选择访问或者不访问,并且当前点的访问价值应该严格大于前一个访问的点。 现在求,从起点出发,到达终点,在时间限制内,能得到的最大

Sorting It All Out POJ(拓扑排序+floyd)

一个就是简单的拓扑排序,一个就是利用floyd判断是否存在环 #include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;#define MAXD 30#define MAX_SIZE 1000vector<int>G[MAXD];int n,m;char L[MAX

最短路径之Floyd_Warshall算法

int d[Max_v][Max_v];//d[u][v]表示权值int V;//顶点数void Floyd(){for(int k = 0; k < V; k++)for(int i = 0; i < V; i++)for(int j = 0; j <V; j++)d[i][j] = min(d[i][j],d[i][k] + d[k][j]);}

巧妙的运用Floyd算法

题目大概意思:输入n,m,n代表n个点,接着输入n个点之间的距离(n*n的矩阵),接下来m次询问,输入a,b,c如果a,b之间的最短路径中存在c点则输出Yes,否则输出No 比赛的时候没有做出来,赛后帆哥一点播就知道了。。。。我写的时候直接用floy算法求距离并记录路径。。然后TLE到死。。。我就奇怪了数据n,m都小于100,怎么会TLE啊。。。坑爹啊。。。我一直怀疑是不是用别的算法。。。。。帆

【POJ】3660 Cow Contest floyd(可以拓扑排序?)

Cow Contest Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6925 Accepted: 3792 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating i

【HDU】1317 XYZZY spfa判负环+floyd求传递闭包

传送门:【HDU】1317 XYZZY 题目分析:首先我们可以用spfa判最长路上是否有正权环,但是有正权环却不等价于能到达终点。这是我们还需要判断是否能从正权环中走到终点,这个可以用传递闭包搞定。如果没有正权环就看是否能从起点到终点就好了。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>

Day54 | Floyd 算法 A * 算法

语言 Java Floyd 算法 题目 97. 小明逛公园 97. 小明逛公园 题目描述 小明喜欢去公园散步,公园内布置了许多的景点,相互之间通过小路连接,小明希望在观看景点的同时,能够节省体力,走最短的路径。  给定一个公园景点图,图中有 N 个景点(编号为 1 到 N),以及 M 条双向道路连接着这些景点。每条道路上行走的距离都是已知的。 小明有 Q 个观景计划,每个计划都有