Shortest Path Problem C (2019 computer)

Problem C city road
time limit 1000 ms 65536 KB memory limit

Title Description

Several road between n cities, some way needs to close the night, the shortest path is determined each city to city n 1 day and night.

Input Format

A first set of behavioral data for each test number T

The first row of three integers, n, m, k. (1 <= n <= 50) n represents the number of cities, m represents the number of roads, k denotes the number of night road need to be closed.

Next m lines of three integers a, b, c (1 < = a, b <= n), where i-th row (1 <= i <= m ) denotes the i-th
road from city to a b urban length C (edge may be duplicates).

Next k lines, each an integer w, represents the number of night to close the road.

Output Format

Two data output lines each

A first daytime behavior shortest distance from city to city n is 1

The first night the behavior of the shortest distance from the city 1 to city n

SAMPLE INPUT

1
4 4 1
1 2 1
2 3 1
3 4 1
1 4 1
4

Sample Output

1
3

Floyd triple loop

#include<bits/stdc++.h>
using namespace std;
int main(){
	int T,n,m,k;
	int a,b,c;
	int w;
	int dis[55][55];
	int dis1[55][55];
	int num[55][2];//记录第几条路 
	cin>>T;
	while(T--){
		cin>>n>>m>>k;
		for(int i=0;i<55;i++){//初始化为不可达 
			for(int j=0;j<55;j++){
				dis[i][j]=99999;
				dis1[i][j]=99999;
			}
		}
		for(int i=1;i<=m;i++){
			cin>>a>>b>>c;//从城市a到城市b长度为c
			dis[a][b]=c;
			dis[b][a]=c;
			
			dis1[a][b]=c;
			dis1[b][a]=c;
			
			num[i][0]=a;
			num[i][1]=b;
		}
		for(int p=1;p<=m;p++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
					if(dis[i][p]!=99999&&dis[p][j]!=99999&&dis[i][j]>dis[i][k]+dis[k][j]){
						dis[i][j]=dis[i][k]+dis[k][j];
					}
				}
			}
		}
		cout<<dis[1][n]<<endl;
		while(k--){
			cin>>w;
			dis1[num[w][0]][num[w][1]]=99999;
			dis1[num[w][1]][num[w][0]]=99999;
		}
		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
					if(dis1[i][k]!=99999&&dis1[k][j]!=99999&&dis1[i][j]>dis1[i][k]+dis1[k][j]){
						dis1[i][j]=dis1[i][k]+dis1[k][j];
					}
				}
			}
		}
		cout<<dis1[1][n]<<endl;
	}
}
Published 67 original articles · won praise 111 · views 60000 +

Guess you like

Origin blog.csdn.net/chaokudeztt/article/details/104826925