23333
Dijikstra算法初体验。
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<cstring>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3;
int d[105],mp[105][105],vis[105];
void diji(int n, int x) {
for(int i =1; i <= n; i++) {
mp[i][i] = 0;
vis[i] = 0;
d[i] = mp[1][i];
}
vis[1] = 1;
for(int i = 1; i <= n; i++) {
int _min = INF;
int p;
for(int j = 1; j <= n; j++) {
if(!vis[j] && _min > d[j]) {
_min = d[j];
p = j;
}
}
vis[p] = 1;
for(int k = 1; k <= n; k++) {
if(!vis[k] && d[k] > d[p] + mp[p][k]) {
d[k] = d[p] + mp[p][k];
}
}
}
}
int main() {
int n,m;
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) != EOF && n && m) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) mp[i][j] = mp[j][i] = INF;
int a,b,c;
while(m--) {
cin >> a >> b >> c;
mp[a][b] = mp[b][a] = c;
}
diji(n,1);
printf("%d\n",d[n]);
}
return 0;
}