Zzuoj 10408:c. Minimum transfer "Shortest way Dijkstra"

Source: Internet
Author: User

10408:c. Minimum transfer time limit:2 Sec Memory limit:128 MB
Submit:31 Solved:8
[Submit] [Status] [Web Board] Description

a city in Europe is a famous tourist destination, with thousands of people visiting every year. Dr. Kong decided to take the summer vacation for a good visit.

young people travel not afraid of hardship, not afraid of exertion, as long as the cost must not line. But Dr. Kong Donglin, who wants to take the bus from the hotel to the attractions they want to visit, during which time they can change their rides less.

Dr. Kon. bought a tourist map. He found that the municipal departments in order to facilitate the tourists, in various tourist attractions and hotels, restaurants and other places have set up a number of bus stations and opened some one-way routes. Each one-way route departs from a bus stop, passing through several stations in turn, eventually reaching the finish station.

but unfortunately, from the hotel where he stayed, some of the attractions can be direct, some attractions cannot direct, he may have to take a road first BUS take a few stops and then come down and transfer to the other side of the same station. BUS, this will require several transfers to get to the attraction.

1 2 , N number. dr. Kong location is numbered 1 n

ask you to help Dr Kong find an optimal ride plan , from accommodation to attractions, transfer in the middle of the least number of times.

Input

First line: K indicates how many sets of test data are available. (2≤k≤8)

Next to each set of test data:

1 line :       m n         m One way bus line, total n station. ( 1<=m<=100 1<n<=500 )

Section 2~m+1 Line: Each line describes a bus line information, from left to right in the order of operation to give the line of all station numbers, adjacent to two station numbers separated by a space.

Output

for each set of test data, output one line, If you cannot reach the attraction from your accommodation on any route, the output "N0" , or the output of the minimum number of times, output 0 It means that there is no need to change trains.

Sample Input
23 76 74 7 3 62 1 3 52 61 3 5 2 6 4 3
Sample Output
2no 
This topic is the general shortest path problem, but the construction of the special pit
1, the input data need to be entered as a string (note absorption carriage return) and then converted to the number
2, When converting to a number, pay attention to the 2-bit three-digit number of the station number
#include <stdio.h> #include <string.h> #define MAX 1100#define inf 0x3ffffffint N,m;char s[max];int vis[510];    int r[510];int low[510],map[510][510];void init () {int i,j;    for (i=1;i<=m;i++) {for (j=1;j<=m;j++) {if (i==j) map[i][j]=0; else Map[i][j]=inf;}}} void Getmap () {int len,i,j,k,t;int Sum;getchar (), while (n--) {gets (s); T=0;for (I=0;i<strlen (s); i++) {if (s[i]!= ') {    Sum=0;while (s[i]!= "&&i<strlen (s))//Consider station number 2 or 3 digits {sum=sum*10+ (s[i]-' 0 '); i++;} R[t++]=sum;}} for (i=0;i<t-1;i++) {for (j=i+1;j<t;j++) {map[r[i]][r[j]]=1;}}} }void Dijkstra () {int I,j,min,next;memset (vis,0,sizeof (VIS)), for (i=1;i<=m;i++) low[i]=map[1][i];vis[1]=1;for (i= 1;i<m;i++) {min=inf;for (j=1;j<=m;j++) {if (!vis[j]&&min>low[j]) {min=low[j];next=j;}} Vis[next]=1;for (j=1;j<=m;j++) {if (!vis[j]&&low[j]>low[next]+map[next][j]) low[j]=low[next]+map[ NEXT][J];}} if (low[m]!=inf) printf ("%d\n", low[m]-1); elseprintf ("no\n");} int main () {int t;scanf ("%d", &AMp;t), while (t--) {scanf ("%d%d", &n,&m); init ();//Initialize Getmap ();//Build Diagram Dijkstra ();//Find Shortest way}return 0;} 

  

Zzuoj 10408:c. Minimum transfer "Shortest way Dijkstra"

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.