matter
1.freopen的使用。非常方便测试。
2.逗号的使用也可以让代码简洁。
code
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 205;
const int INF = 10000000;
int G[MAXN][MAXN];
int main(){
freopen("test.txt" , "r" , stdin);
int n , m , k , l , c1 , c2 , dis , min_d = INF , min_index;
fill(G[0] , G[0] + MAXN * MAXN , INF);
scanf("%d %d" , &n , &m);
for(int i = 0 ; i < m ; i ++){
scanf("%d %d %d" , &c1 , &c2 , &dis);
G[c1][c2] = G[c2][c1] = dis;
}
scanf("%d" , &k);
for(int i = 1 ; i <= k ; i ++){
int flag = 0, cnt = 0 , d = 0;
printf("Path %d: " , i);
scanf("%d %d" , &m , &c1);
vector<int> vis;
vis.resize(m);
while(-- m){
scanf("%d" , &c2);
if(G[c1][c2] < INF || flag == 2){
if(vis[c2] == 0) vis[c2] = 1 , cnt ++;
else flag = 1;
d += G[c1][c2];
c1 = c2;
}
else flag = 2;
}
if(flag != 2) printf("%d " , d);
else printf("NA ");
if(cnt == n && flag == 0) printf("(TS simple cycle)\n");
else if(flag == 1 && cnt == n) printf("(TS cycle)\n");
else printf("(Not a TS cycle)\n");
if(flag != 2 && cnt == n && min_d > d) min_index = i , min_d = d;
}
printf("Shortest Dist(%d) = %d" , min_index , min_d);
return 0;
}