You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 110 ;
# define INF 0x7ffffff
/**
分析:模板题, 理解floyd 的在 i , j 路径中没有包含k( 因为此时k未用来更新) , 即可写出最小环
在INF这里wa了两发, 习惯值 1e9 不是最大值(查找时判断一下可以避免,但。。。懒了)
*/
int n , m ;
int g [ N ] [ N ] ;
int dis [ N ] [ N ] ;
int path [ N ] [ N ] ;
int ans [ N ] ;
int cnt ;
int mm ;
void floyd ( ) {
mm = INF ;
for ( int k = 1 ; k < = n ; k + + ) {
// dp
for ( int i = 1 ; i < k ; i + + ) {
for ( int j = i + 1 ; j < k ; j + + ) {
int x = dis [ i ] [ j ] + g [ k ] [ i ] + g [ k ] [ j ] ;
if ( x < mm ) {
mm = x ;
int tg = j ;
cnt = 0 ;
while ( tg ! = i ) {
ans [ cnt + + ] = tg ;
tg = path [ i ] [ tg ] ;
}
ans [ cnt + + ] = i ;
ans [ cnt + + ] = k ;
}
}
}
// floyd
for ( int i = 1 ; i < = n ; i + + )
for ( int j = 1 ; j < = n ; j + + )
if ( dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] ) {
dis [ i ] [ j ] = dis [ i ] [ k ] + dis [ k ] [ j ] ;
path [ i ] [ j ] = path [ k ] [ j ] ; // 这咋还和我理解的不一样呢?
}
}
}
int main ( ) {
while ( cin > > n > > m ) {
// 邻接矩阵初始化
for ( int i = 1 ; i < = n ; i + + )
for ( int j = 1 ; j < = n ; j + + ) {
dis [ i ] [ j ] = g [ i ] [ j ] = INF ;
path [ i ] [ j ] = i ;
}
// 读入边
while ( m - - ) {
int a , b , c ;
cin > > a > > b > > c ;
g [ a ] [ b ] = g [ b ] [ a ] = min ( g [ a ] [ b ] , c ) ;
dis [ a ] [ b ] = dis [ b ] [ a ] = g [ a ] [ b ] ;
}
floyd ( ) ;
if ( mm = = INF ) {
puts ( " No solution. " ) ;
continue ;
}
for ( int i = 0 ; i < cnt ; i + + ) printf ( " %d%s " , ans [ i ] , ( i = = cnt - 1 ) ? " \n " : " " ) ;
}
return 0 ;
}