#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
#define MAXN 102
#define INF 0x1e8
struct edge {
int fromCity;
int toCity;
int length;
struct edge(int u, int v, int w) { fromCity = u; toCity = v; length = w; }
};
typedef struct edge Edge;
typedef struct vertex {
int pi;
int weight;
}Vertex;
int edges[MAXN][MAXN];
int originalEdges[MAXN][MAXN];
int verticesPi[MAXN][MAXN];
Vertex vertices[MAXN];
vector<int > path;
int M, N;
void init()
{
memset(verticesPi, 0xff, sizeof(verticesPi));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
originalEdges[i][j] = (i == j) ? 0 : INF;
}
path.clear();
}
void input()
{
int place1, place2, length;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &place1, &place2, &length);
if (length < originalEdges[place1 - 1][place2 - 1]) {
verticesPi[place1 - 1][place2 - 1] = place1 - 1;
verticesPi[place2 - 1][place1 - 1] = place2 - 1;
originalEdges[place1 - 1][place2 - 1] = originalEdges[place2 - 1][place1 - 1] = length;
}
}
}
void floydWarshall()
{
int minCircle = INF;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
edges[i][j] = originalEdges[i][j];
for (int k = 0; k < N; k++) {
for (int i = 0; i < k; i++) {
for (int j = i + 1; j < k; j++) {
int D = edges[i][j] + originalEdges[i][k] + originalEdges[j][k];
if (D < minCircle) {
minCircle = D;
path.clear();
int t = j;
path.push_back(j);
while (verticesPi[i][t] != i) {
path.push_back(verticesPi[i][t]);
t = verticesPi[i][t];
}
path.push_back(i);
path.push_back(k);
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (edges[i][j] > edges[i][k] + edges[k][j])
verticesPi[i][j] = verticesPi[k][j];
edges[i][j] = min(edges[i][j], edges[i][k] + edges[k][j]);
}
}
}
if (minCircle >= INF)
printf("No solution.\n");
else
for (int i = 0; i < path.size();i++)
printf("%d%c", path[i] + 1, (i == path.size() - 1) ? '\n':' ');
}
int main()
{
while (scanf("%d", &N) && ~N) {
scanf("%d", &M);
init();
input();
floydWarshall();
}
return 0;
}
one from ural
最新推荐文章于 2024-05-27 11:02:46 发布