poj 1125 谣言传播 Floyd 模板题

假如有3个点 点1到点2要5分钟 点1到点3要3分钟 那么5分钟的时间可以传遍全图 所以要先找一个点到其他点的最长时间 再从最长的时间里找出最小值

Sample Input

3 // 结点数
2 2 4 3 5 //点1与另外2个点相连 1->2 权值为4 1->3 权值为5
2 1 2 3 6 //点2与...
2 1 2 2 2//点3...
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

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <string>
 5 # include <algorithm>
 6 # include <cmath>
 7 # include <map>
 8 # define LL long long
 9 using namespace std ;
10
11 const int MAXN = 110 ;
12 const int INF = 0x3f3f3f3f;
13 int dis[MAXN][MAXN];
14 int n ;
15
16 void floyed()//节点从1~n编号
17 {
18     int i,j,k;
19     for(k=1;k<=n;k++)
20        for(i=1;i<=n;i++)
21          for(j=1;j<=n;j++)
22              if(dis[i][k]+dis[k][j] < dis[i][j])
23                  dis[i][j]=dis[i][k]+dis[k][j];
24
25 }
26
27 int main()
28 {
29     //freopen("in.txt","r",stdin) ;
30     while (scanf("%d" , &n) , n)
31     {
32         int i , j ;
33         for(i=1;i<=n;i++)
34           for(j=1;j<=n;j++)
35           {
36               if(i==j)dis[i][j]=0;
37               else dis[i][j]=INF;
38           }
39         int m ,v ,w ;
40         for(i=1;i<=n;i++)
41         {
42             scanf("%d" , &m) ;
43             while(m--)
44             {
45                 scanf("%d %d" , &v , &w) ;
46                 if (w < dis[i][v])
47                     dis[i][v] = w ;
48             }
49         }
50         floyed() ;
51         int ans = INF ;
52         int k ;
53         for(i=1;i<=n;i++)
54         {
55             int t = 0 ;
56             for(j=1;j<=n;j++)
57             {
58                 if (i != j && dis[i][j] > t)
59                 {
60                     t = dis[i][j] ;
61                 }
62             }
63             if (t < ans)
64             {
65                 ans = t ;
66                 k = i ;
67             }
68         }
69         if (ans != INF)
70            printf("%d %d\n" , k , ans) ;
71         else
72            printf("disjoint\n") ;
73     }
74     return 0;
75 }

时间: 2024-10-08 15:25:18

poj 1125 谣言传播 Floyd 模板题的相关文章

POJ 1125 Stockbroker Grapevine (Floyd算法)

Floyd算法计算每对顶点之间的最短路径的问题 题目中隐含了一个条件是一个人可以同时将谣言传递给多个人 题目最终的要求是时间最短,那么就要遍历一遍求出每个点作为源点时,最长的最短路径长是多少,再求这些值当中最小的是多少,就是题目所求 #include<bits/stdc++.h> using namespace std; int n,x,p,t; int m[120][120],dist[120][120],Max[120]; void floyd(int n,int m[][120],int

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

poj 3580 SuperMemo splay树模板题

题意: 给一个序列,对其进行各种操作.在对序列仅对操作的处理上,splay是比线段树强大的,虽然均摊复杂度均为logN,但它能支持1:在某个位置插入一些连续的数,2:在某个位置删除一些连续的数.只是splay随便一些200+行. 分析: 网上各种模板介绍漫天飞,这个还算简洁明了. 代码: //poj 3580 #include <stdio.h> #define maxN 200000 int N,T,node; int a[maxN],size[maxN],left[maxN],right[

POJ 1502 MPI Maelstrom (Dijkstra 模板题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5877   Accepted: 3654 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1269 - Intersecting Lines - [平面几何模板题]

题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection b

poj 1125 Stockbroker Grapevine -- floyd 全源最短路

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33164   Accepted: 18259 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

POJ 1125 Stockbroker Grapevine Floyd 最短路

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

POJ 1287 Networking【MST模板题】

Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8214 Accepted: 4528 Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possib