POJ 2387 Til the Cows Come Home 最短路 Dijstra
2017-05-13 17:13
405 查看
题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离。。
思路:dijkstra求单源最短路,但是要注意判重。
思路: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]; 4000 } } 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]; } } }
相关文章推荐
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- 第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
- POJ 2387 Til the Cows Come Home --最短路模板题
- POJ 2387 Til the Cows Come Home 【最短路(dijkstra)】
- POJ 2387 Til the Cows Come Home(迪杰斯特拉/优先队列/最短路)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ 2387 Til the Cows Come Home 最短路
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
- 7_4_N题 Til the Cows Come Home 题解[poj 2387] (最短路)
- POJ 2387 Til the Cows Come Home(最短路dijkstra)
- poj 2387 Til the Cows Come Home(最短路SPFA+Dijkstra)
- POJ-2387 Til the Cows Come Home ( 最短路 )
- 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(kuangbin带你飞 专题四:最短路)
- POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- Dijstra算法:Til the Cows Come Home POJ - 2387
- POJ 2387 - Til the Cows Come Home(单源最短路)