2019_GDUT_ newborn thematic map on the B - Shortest

topic

In the annual school competition, all students finalists will receive a very nice t-shirt. But whenever our staff to hundreds of pieces of clothing shipped back to the stadium from the store when they are very tired! So now they want to find the shortest route to the stadium from the store, you can help them?

Input
input includes a plurality of sets of data. The first line of each two integers N, M (N <= 100 , M <= 10000), N represents a few street intersections Chengdu, reference numeral 1 is to store the location of the intersection, the intersection designated N is the location of the stadium, M indicates a couple of choices in Chengdu. N = M = 0 indicates the end of input. Next M rows, each row comprising three integers A, B, C (1 < = A, B <= N, 1 <= C <= 1000), indicates there is a path between the intersection A and the intersection B, we staff need to C-minute time traveled this road.
Input line to ensure the presence of at least one track to store. Output For each input and output line, represents the track workers come from the store minimum time

Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3 2

Most short-circuit evaluation point, the standard dijkstra algorithm. Remember to visit initialize the array.

Code:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,a[1000][1000];

int main()
{
	int A,B,c,d[10100],visit[10100];
	cin>>n>>m;
	while (n!=0 && m!=0)
	{
		memset(a,0x3f,sizeof(a));
		a[1][1]=0;
		for (int i=1;i<=m;i++)
		{
			cin>>A>>B>>c;
			a[A][B]=c;
			a[B][A]=c;
		}
		for (int i=1;i<=n;i++)
			d[i]=INF;
		memset(visit,0,sizeof(visit));
		d[1]=0;
		for (int i=1;i<=n;i++)
		{
			int mi=INF,x=-1;
			for (int j=1;j<=n;j++)
			if (visit[j]==0 && d[j]<mi)
			{
				mi=d[j];
				x=j;
			}
			if (x==-1) break;
			visit[x]=1;
			for (int j=1;j<=n;j++)
			{
				d[j]=min(d[j],d[x]+a[x][j]);
			}
		}
		cout<<d[n]<<endl;
		cin>>n>>m;
	}
	return 0;
}
Published 14 original articles · won praise 0 · Views 288

Guess you like

Origin blog.csdn.net/qq_39581539/article/details/104011626