穿越隧道
朴素和优化的dijkstra都可。 目前,只有朴素,后续优化待补
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 2510;
int g[N][N];
int a,b;
int n;
int dis[N];
bool vis[N];
int t,c,s,e;
void dijkstra(){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s] = 0;
for(int i = 1; i <= t; i++){
int tt = -1;
for(int j = 1; j <= t; j++){
if(!vis[j] && (tt == -1 || dis[tt] > dis[j])){
tt = j;
}
}
vis[tt] = true;
for(int j = 1; j <= t; j++){
dis[j] = min(dis[j],dis[tt] + g[tt][j]);
}
}
}
int main(){
scanf("%d%d%d%d",&t,&n,&s,&e);
memset(g,0x3f,sizeof(g));
while(n--){
scanf("%d%d%d",&a,&b,&c);
g[a][b] = g[b][a] = min(g[a][b],c);
g[b][a] = min(g[b][a],c);
}
dijkstra();
printf("%d\n",dis[e]);
return 0;
}
|