【九度OJ】1035【Floyd算法】
来源:互联网 发布:看网球软件 编辑:程序博客网 时间:2024/05/20 16:45
抽象成图。
代码:
package Test1;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.StreamTokenizer;public class Test30_1035 {public static void main(String[] args) throws IOException {StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));// Floyd算法是一个经典的动态规划算法while (true) {st.nextToken();int n = (int) st.nval;st.nextToken();int m = (int) st.nval;if (n == 0 && m == 0)break;int dist[][] = new int[27][27]; // 1--->26for (int i = 1; i <= 26; i++){for (int j = 1; j <= 26; j++){dist[i][j] = 1000000; //设为Integer.MAX_VALUE 相加时会溢出}dist[i][i]=0;}String str;int child, parent1, parent2;for (int i = 0; i < n; i++) {st.nextToken();str = st.sval;child = str.charAt(0) - 64; // 从1开始parent1 = str.charAt(1) - 64;parent2 = str.charAt(2) - 64;if(parent1<=26 && parent1>=1)dist[parent1][child]=1; //有向图 父母指向孩子if(parent2<=26 && parent2>=1)dist[parent2][child]=1;}for(int k=1;k<=26;k++)for(int i=1;i<=26;i++)for(int j=1;j<=26;j++){if(dist[i][k]+dist[k][j]<dist[i][j]){dist[i][j]=dist[i][k]+dist[k][j];}}int people1, people2;for (int i = 0; i < m; i++) {st.nextToken();str = st.sval;people1 = str.charAt(0) - 64;people2 = str.charAt(1) - 64;if(dist[people1][people2]!=1000000){print(dist[people1][people2],1);}else if(dist[people2][people1]!=1000000){print(dist[people2][people1],0);}else{System.out.println("-");}}}}private static void print(int i,int flag) {if(i==1){if(flag==1)System.out.println("child");elseSystem.out.println("parent");}else if(i==2){if(flag==1)System.out.println("grandparent");elseSystem.out.println("grandchild");}else{int n=i-2;for(int j=0;j<n;j++)System.out.print("great-");if(flag==1)System.out.println("grandparent");elseSystem.out.println("grandchild");}}}
0 0
- 【九度OJ】1035【Floyd算法】
- 九度 OJ 题目1447:最短路径(Floyd 算法)
- 九度OJ-1447:最短路(Floyd)
- 九度OJ-题目1447-最短路径-floyd-dijisitra
- 九度oj 1035
- 九度OJ 1082 代理服务器 -- 贪心算法
- 九度OJ 1433 FatMouse -- 贪心算法
- 九度OJ 1433 贪心算法
- 九度OJ:1343 城际公路网(特定情况下的floyd优化)
- 【九度OJ】:九度OJ 1050
- 【九度OJ】:九度OJ 1053
- 【九度OJ】:九度OJ 1056
- 【九度OJ】:九度OJ 1059
- 【九度OJ】:九度OJ 1206
- 九度oj 1037 Powerful Calculator 高精度算法
- 九度OJ 教程91 回溯算法之《全排列》
- 九度OJ 1514 数值的整数次方【算法】
- 九度OJ 1214 寻找丑数【算法】
- 线段树+离散化 IP地址段检查 SEGMENT TREE
- 哭的时候,自己都不认识自己
- java实现的des加密
- Hadoop2.0完全分布式安装(无HA 联邦)
- 关于做Android+J2ee系统集成开发的一点心得
- 【九度OJ】1035【Floyd算法】
- 2014年8月17日晚
- 百度语音识别的文档链接
- 445B - DZY Loves Chemistry
- Win32_Sec day
- 程序员生存定律-六个程序员的故事(1)
- atitit. groupby linq的实现(1)-----linq框架选型 java .net php
- TCP/IP 学习答疑
- 堆排序