Home Article Practice HihoCoder 1081dijkstra最短路算法模板

HihoCoder 1081dijkstra最短路算法模板

2020-03-21 10:47  views:1708  source:zsnoip    

#include<iostream>
#include<cstring>
using namespace std;
const int N = 1010 , M = 20010 ;
int n , m , s , t;
int g[N][N] , dist[N];
bool vis[N];
void dijkstra(int s){
memset(dist , 0x3f , sizeof(dist));
memset(vis , 0 , sizeof(vis));
dist[s] = 0;
for(int i = 1 ; i < n ; i++){
int x = 0;
for(int j = 1 ; j <= n ; j ++){
if(!vis[j] && (x == 0 || dist[j] < dist[x])) x = j ;
}
vis[x] = 1;
for(int y = 1 ; y <= n ; y++){
dist[y] = min(dist[y] , dist[x] + g[x][y]);
}
}
}
int main(){
cin >> n >> m >> s >> t;
memset(g , 0x3f , sizeof(g));
for(int i = 1; i <= n ; i++)
g[i][i] = 0;
for(int i = 1; i <=m ; i++){
int x , y , z;
cin >> x >> y >> z;
g[x][y] = min(g[x][y] , z);
g[y][x] = min(g[y][x] , z);
}
dijkstra(s);
cout << dist[t] << endl;
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)