POJ 2387 Til the Cows Come Home 最短路 Dijstra
来源:互联网 发布:项目 书籍 商务 知乎 编辑:程序博客网 时间:2024/05/20 03:39
题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离。。
思路:dijkstra求单源最短路,但是要注意判重。
import java.util.*; public class Main{ public static void main(String args[]){ Scanner in=new Scanner(System.in); int c=0; int t=in.nextInt();//输入顺序错误WA了N次!!!看清题意! int n=in.nextInt(); int dist[]=new int[n+1];//先输入再确定数组大小!!!RE了N次! int[][] w=new int[n+1][n+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ w[i][j]=Integer.MAX_VALUE; } } for(int i=0;i< t;i++){//输入两点及之间距离 int sn=in.nextInt(); int en=in.nextInt(); c=in.nextInt(); if(c< w[sn][en]) w[sn][en]=w[en][sn]=c; } dijkstra(n,w,dist); System.out.println(dist[1]); } public static void dijkstra(int v ,int[][] a, int[] dist){//最短路径dijkstra算法 int nn = dist.length - 1 ; boolean[] s = new boolean[nn+1]; //初始化 for(int i = 1; i <= nn; i++) dist[i] = a[v][i]; dist[v] = 0; s[v] = true; for(int i = 1; i < nn; i++){ int temp =Integer.MAX_VALUE; int u = v; for(int j = 1; j < nn; j++){ if(!s[j] && dist[j] < temp&&dist[j]>0){ u = j; temp = dist[j]; } } s[u] = true; //找到了第一个并入S的节点 for(int j = 1; j <= nn; j++) if(!s[j] &&dist[u]+a[u][j]< dist[j]&& a[u][j] < Integer.MAX_VALUE) dist[j] =dist[u]+a[u][j]; } } }
0 0
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- POJ 2387 Til the Cows Come Home 最短路 Dijstra
- 最短路 (Dijstra) Til the Cows Come Home
- POJ 2387Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- poj 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home 最短路
- POJ 2387 Til the Cows Come Home【最短路】
- POJ 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- POJ-2387 Til the Cows Come Home【最短路】
- POJ-2387-Til the Cows Come Home [最短路]
- poj Til the Cows Come Home (Dijkstra最短路~)
- 最短路----Til the Cows Come Home
- Til the Cows Come Home 【最短路】
- Til the Cows Come Home 【最短路】
- 平方数
- js输出当前系统时间
- Note4、const
- redis 三(数据类型)
- SSM项目---淘淘商城(IDEA开发)
- POJ 2387 Til the Cows Come Home 最短路 Dijstra
- Java内存模型
- [leetcode: Python]290.Word Pattern
- linux shell
- 关于tomcat发布项目404的奇葩问题
- BZOJ3173 [Tjoi2013]最长上升子序列
- 玲珑杯-No Game No Life
- 使用IDEA创建maven父子工程
- 什么是回调函数?