poj 1502

题意:
给你一个不完全的矩阵,数字表示权值,x表示两点间不可达
由于自身到自身花费的时间为0,所以没有给出,由于i到j和j到i距离相同,互达时间相同
所以只给出了一半的临界矩阵。
根据给你的这个临界矩阵,让你来求从点1到其他点所花费最短时间集里面的的最大值。
一个很直接的最短路

三种单源点最短路径算法都练习了以下,dijkstra,bellman-ford,SPFA

 1 import java.util.Scanner;
 2 import java.util.Arrays;
 3 /*
 4 dijkstra
 5 */
 6 public class Main{
 7     static int INF = 0x3f3f3f3f;
 8     static int n;
 9     static boolean visited[];
10
11     public static void dij(int map[][],int dist[]){
12         visited[1] = true;
13         int u = 0;
14         for(int i=1;i<n;i++){
15             int min = INF;
16             for(int j=2;j<=n;j++){
17                 if(visited[j]==false&&min>dist[j]){
18                     min = dist[j];
19                     u = j;
20                 }
21             }
22             visited[u] = true;
23             for(int k=2;k<=n;k++){
24                 if(visited[k]==false&&dist[u]+map[u][k]<dist[k])
25                     dist[k] = dist[u]+map[u][k];
26             }
27         }
28     }
29
30     public static void main(String[] args){
31         Scanner in = new Scanner(System.in);
32         n = in.nextInt();
33         in.nextLine();
34         int map[][] = new int[n+1][n+1];
35         for(int i=0;i<=n;i++)
36             Arrays.fill(map[i],INF);
37
38         for(int i=2;i<=n;i++){
39             String nums[] = in.nextLine().split(" ");
40             for(int j=1;j<i;j++){
41                 if(nums[j-1].equals("x"))
42                     continue;
43                 map[i][j]=map[j][i] = Integer.parseInt(nums[j-1]);
44             }
45         }
46         int dist[] = new int[n+1];
47         for(int i=2;i<=n;i++)
48             dist[i] = map[1][i];
49         visited = new boolean[n+1];
50         dij(map,dist);
51         int max=0;
52         for(int i=2;i<=n;i++)
53             if(dist[i]>max)
54                 max = dist[i];
55         System.out.println(max);
56     }
57 }
 1 import java.util.Scanner;
 2 import java.util.Arrays;
 3 /*
 4 bellman-ford
 5 */
 6 public class Main{
 7     static int INF = 0x3f3f3f3f;
 8     static int n;
 9
10     public static void bf(int map[][],int dist[],int v){
11         for(int k=2;k<n;k++)
12             for(int u=1;u<=n;u++)
13                 if(u!=v)
14                     for(int i=1;i<=n;i++)
15                         if(i!=v&&dist[u]>dist[i]+map[i][u])
16                             dist[u] = dist[i]+map[i][u];
17
18     }
19
20     public static void main(String[] args){
21         Scanner in = new Scanner(System.in);
22         n = in.nextInt();
23         in.nextLine();
24         int map[][] = new int[n+1][n+1];
25         for(int i=0;i<=n;i++)
26             Arrays.fill(map[i],INF);
27
28         for(int i=2;i<=n;i++){
29             String nums[] = in.nextLine().split(" ");
30             for(int j=1;j<i;j++){
31                 if(nums[j-1].equals("x"))
32                     continue;
33                 map[i][j]=map[j][i] = Integer.parseInt(nums[j-1]);
34             }
35         }
36         int dist[] = new int[n+1];
37         for(int i=2;i<=n;i++)
38             dist[i] = map[1][i];
39         bf(map,dist,1);
40         int max=0;
41         for(int i=2;i<=n;i++)
42             if(dist[i]>max)
43                 max = dist[i];
44         System.out.println(max);
45     }
46 }
时间: 2024-08-07 06:36:38

poj 1502的相关文章

poj 1502 最段路径

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} poj 1502 最段路径 连接:http://poj.org/problem?id=1502 5 50 30 5 100 20 50 10 x x 10 第一行表示有 n 个点,之后第二行的第一个数表示 (2,1)的距离:第三行的第一个数表示 (3,1),第二个数表示 (3,2).以此类推

poj 1502 最短路+坑爹题意

链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5249   Accepted: 3237 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed share

poj 1502 MPI Maelstrom(最短路)

poj 1502 MPI Maelstrom Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swige

poj 1502 MPI Maelstrom

题目链接: http://poj.org/problem?id=1502 题目大意: 有一个信息传递系统,含有n个处理器,传递信息的方式是:刚开始编号为1的处理器拥有信息,他可以传给下一个处理器,然后这两个拥有信息的处理器可以同时向下传递给其他两个处理器,拥有信息的四个处理器再依次如此传递,直到所有处理器都接受到信息的最短时间是多少? 解题思路: 把传递路径画出来,看出可以转化成求从1到其他位置的最短路径中的最长路径,刚看到题目感觉没什么思路,但是建立出来模型以后用dijkstra就好啦! 1

MPI Maelstrom POJ 1502

http://poj.org/problem?id=1502 题意:这是一个下三角,上三角跟下三角图形一致,(若是完整的矩阵的话, 相当于 Map[i][j] 的距离为相对应的数值)这道题也一样. 不同的是 若 显示的为 ‘x’字母时, 说明Map[i][j]为正无穷, 两个点之间不通. 现在的问题是:求1到2, 1到3, .... 1到n 之中哪条路是最短的. **** 英语真的是太渣,表示看懂题意不是一般的难啊, 但是看懂题意后真的好简单 %>_<% .. 不要再考我英语了,我诚实的说四级

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 1502 MPI Maelstrom Dijkstra算法的简单运用 ,呵呵,,我估计有很多人都没看懂什么意思,我也看了很久

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

[poj 1502]昂贵的聘礼

一道不算太难的最短路喵~ 容我吐槽一下,酋长的地位居然不是最高的额——那你特么的居然还算是酋长?! 枚举一个地位区间 [i..i+M-1] 只要所有的交易者的地位都在该区间中,那么就不会引起冲突 而这个可悲的酋长是必须在区间中的,所以若酋长的地位为 L0 那么该枚举的区间就是 [L0-i, L0+M-i] {i|0<=i<=M} 然后就是裸的 dijkstra 了,取出地位在区间中的点,然后新增一个点 N+1,向所有点连边,距离是所有物品的直接价格 若 物品 i 可以通过 物品 j 来降价,那

kuangbin_ShortPathG (POJ 1502)

尽管题目很长 写的很玄乎 让我理解了半天 但是事实上就是个模板题啊摔 一发水过不解释 #include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <queue> #include <map> #include <vector> #include <set> #

POJ 1502 水 dij

题意:给N,表示N个节点. 给半个邻接矩阵,本身到本身的距离是0,边是双向的.当两个节点之间没有直接的边连接的时候,用x表示. 问从第一个节点到其他所有节点至少花费的时间. 这题唯一的处理是处理邻接矩阵的时候处理先当作字符串读入,然后处理一下数据,变成数据格式. 最后是输出第一个节点到其他所有节点最短路的最大值. #include<stdio.h> #include<string.h> int n; const int inf=99999999; int pho[105][105]