City chaos _ nyoj_115 (Dijkstra). Java

Source: Internet
Author: User
City chaos time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description

General Nan leads n troops in different cities.

He is using the N troops to maintain the security of M cities, which are numbered from 1 to M.

Now, the minor engineer told General Nan that a riot occurred in city K, and general Nan dispatched a detachment from all the troops along the nearest road to the turbulent city.

Now we know the time required for marching between any two cities. You are the most powerful programmer under general Nan, please write a program to tell General Nan the time required for the first detachment to arrive in the rebel city.

Note that there may be more than one road between two cities.

Input
Enter an integer T in the first line to indicate the number of test data groups. (T <20)
The first row of each group of test data is four integers: N, M, P, Q (1 <= n <= 100, n <= m <= 1000, m-1 <= P <= 100000) Where n indicates the number of troops, M indicates the number of cities, P indicates the number of roads between cities, and Q indicates the number of cities in which violence occurred.
The next line is n integers, indicating the number of the city where the troops are located.
In the next P row, each row has three positive integers, a, B, T (1 <= A, B <= m, 1 <= T <= 100), indicating, the path between B is T if it is needed

Data ensures that cities in turmoil are reachable.

Output
For each group of test data, the time when the first army arrived in the rebel city was output. Each group of output occupies one row
Sample Input
13 8 9 81 2 31 2 12 3 21 4 22 5 33 6 24 7 15 7 35 8 26 8 2 
Sample output
4
Source
Adapted from "World College Student Program Design Competition advanced tutorial · Book 1"

//AC#include<stdio.h>#include<string.h>#define MAX 1000000int city[101],map[1010][1010],d[1010],vis[1010],m;void dijkstra(int start){for(int i=1;i<=m;i++){d[i]=map[start][i];}d[start]=0;for(int i=1;i<=m;i++){int min = MAX;int x=0;for(int j=1;j<=m;j++){if(!vis[j]&&min>=d[j]){min=d[j];x=j;}}vis[x]=1;for(int j=1;j<=m;j++){if(d[j]>min+map[x][j]){d[j]=min+map[x][j];}}}}int main(){int N,i,j;scanf("%d",&N);while(N--){int n,p,q;scanf("%d%d%d%d",&n,&m,&p,&q);for(i=0;i<n;i++)scanf("%d",&city[i]);memset(map,MAX,sizeof(map));memset(vis,0,sizeof(vis));for(i=0;i<p;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(map[a][b]>c)map[a][b]=map[b][a]=c;}int temp=MAX;dijkstra(q);for(i=0;i<n;i++)if(temp>d[city[i]])temp=d[city[i]];printf("%d\n",temp);}return 0;}

import java.util.Arrays;import java.util.Scanner;public class Main{//RuntimeError -- --java08-20 21:45:11static int city[],MAX=1000000,m;static int map[][],d[];static boolean vis[];public static void main(String[] args) {Scanner input=new Scanner(System.in);int N=input.nextInt();while(N-->0){int n=input.nextInt();m=input.nextInt();int p=input.nextInt();int q=input.nextInt();city=new int[n+1];vis=new boolean[m+5];map=new int[m+5][m+5];d=new int[m+5];for(int i=0;i<n;i++)city[i]=input.nextInt();for(int i=1;i<=m;i++){Arrays.fill(map[i],MAX);}for(int i=0;i<p;i++){int a=input.nextInt();int b=input.nextInt();int c=input.nextInt();if(map[a][b]>c)map[a][b]=map[b][a]=c;}int temp=MAX;dijkstra(q);for(int i=0;i<n;i++){if(temp>d[city[i]])temp=d[city[i]];}System.out.println(temp);}}private static void dijkstra(int start) {for(int i=1;i<=m;i++){d[i]=map[start][i];}d[start]=0;for(int i=1;i<=m;i++){int min = MAX;int x=0;for(int j=1;j<=m;j++){if(!vis[j]&&min>=d[j]){min=d[j];x=j;}}vis[x]=true;for(int j=1;j<=m;j++){if(d[j]>min+map[x][j]){d[j]=min+map[x][j];}}}}}

I have not found any errors in the running time of Java so far. If you have found any errors, please let me know. Thank you very much!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.