HDu2680(Choose the best route)
来源:互联网 发布:大二层和三层网络区别 编辑:程序博客网 时间:2024/05/16 08:44
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
package graph_ShortestPath;//思路:构建反向图。import java.util.*;import java.io.*;public class HDU2680 {static int[][]map;static int[]dist;static int n;public static void main(String[] args) throws IOException {//Scanner sc = new Scanner(System.in);StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));int m,s;while(st.nextToken()!=StreamTokenizer.TT_EOF){n = (int)st.nval;st.nextToken();m = (int)st.nval; st.nextToken();s = (int)st.nval;map = new int[n+1][n+1];for (int i = 1; i <= n; i++)Arrays.fill(map[i], Integer.MAX_VALUE);dist = new int[n+1];for(int i = 0;i<m;i++){st.nextToken();int p = (int)st.nval;st.nextToken();int q = (int)st.nval;st.nextToken();int t = (int)st.nval;if(t<map[q][p])map[q][p] = t;//反向图。}st.nextToken();int num =(int)st.nval;int min = Integer.MAX_VALUE;int[]arr = new int[num+1];for(int i = 1;i<=num;i++){st.nextToken();arr[i] =(int)st.nval;}dijkstra(s);for(int i = 1;i<=num;i++){if(dist[arr[i]]<min)min = dist[arr[i]];}if(min>=Integer.MAX_VALUE)System.out.println(-1);else System.out.println(min);}}private static void dijkstra(int ss) {boolean[] p = new boolean[n+1];for(int i = 1;i<=n;i++){p[i] = false;if(i!=ss)dist[i] = map[ss][i];}p[ss] = true;dist[ss] = 0;for(int i = 0;i<n-1;i++){int min = Integer.MAX_VALUE;int k = -1;for(int j = 1;j<=n;j++){if(!p[j] && dist[j]<min){min = dist[j];k = j;}}if(k==-1)return;p[k] = true;for(int j = 1;j<=n;j++){if(!p[j] && map[k][j]!=Integer.MAX_VALUE && dist[j] > dist[k] +map[k][j])dist[j]=dist[k]+map[k][j];}}}}
- HDu2680(Choose the best route)
- hdu2680 Choose the best route (dijkstra)
- HDU2680 Choose the best route
- hdu2680 Choose the best route
- HDU2680-Choose the best route
- HDU2680 Choose the best route
- hdu2680 choose the best route
- hdu2680 Choose the best route 最短路(Dijkstra算法)
- hdu2680—Choose the best route(spfa)
- HDU2680 Choose the best route(最短路)
- hdu2680 Choose the best route (dijkstra)
- hdu2680最短路Choose the best route
- Choose the best route hdu2680 Dijkstra
- hdu2680 Choose the best route (Dijkstra)
- HDU2680:Choose the best route(Dijkstra)
- HDU2680:Choose the best route(Dijkstra)
- HDU2680 Choose the best route 【Dijkstra】
- HDU2680 Choose the best route 单源最短路径
- 有用的CSS技巧
- 如何使用JS来改变CSS样式
- 系统存储方式(分页,分段,虚拟存储)死锁
- Struts1和Struts2的区别
- Java 编程思想(第四版)学习笔记(9)接口
- HDu2680(Choose the best route)
- VB制作BHO写下载器或病毒会用到
- Android中HTTP请求出现“Received authentication challenge is null”异常和“Read timed out”解决
- key_t IPC键和ftok函数详解和剖析
- 使用SQLServer2005的链接服务器链接Sybase数据库
- python的logging.config使用详解
- PJSUA命令行分析
- ubuntu的终端无法输入字母A
- 关于Segmentation fault (core dumped)几个简单问题