C - Heavy Transportation POJ - 1797[dijkstra]
阿新 • • 發佈:2018-12-13
題意:從1出發到各點的最短距離。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define INF 9999999
#define maxn 1005
int MAP[maxn][maxn], Dis[maxn];
bool book[maxn];
int n, m;
void Dijistra();
int main()
{
std::ios::sync_with_stdio(false);
int T, u, v, w;
cin >> T;
book[0] = 0;
for(int Q = 1; Q <= T; Q++)
{
for(int j = 1; j < maxn; j++)
{
book[j] = 0;
Dis[j] = 0;
for(int k = 1; k < maxn; k++)
{
MAP[j][k] = 0;
}
}
cin >> n >> m;
for(int i = 0; i < m; i++)
{
cin >> u >> v >> w;
// if(w < MAP[u][v])
// {
MAP[u][v] = w;
MAP[v][u] = w;
// }
}
for(int i = 1; i <= n; i++)
{
Dis[i] = MAP[1][i];
book[i] = 0;
}
// for(int i = 0; i <= n; i++)
// {
// cout << "#" << MAP[1][i] << endl;
// }
Dijistra();
cout <<"Scenario #" << Q << ":" << endl;
cout << Dis[n] << endl << endl;
}
return 0;
}
void Dijistra()
{
book[1] = 1;
for(int i = 0; i < n; i++)
{
int MIN = -1, u;
for(int j = 1; j <= n; j++)
{
if(book[j] == 0 && Dis[j] > MIN)
{
MIN = Dis[j];
u = j;
}
}
book[u] = 1;
for(int j = 1; j <= n; j++)
{
if(book[j] == 0 && Dis[j] < min(Dis[u], MAP[u][j]))
{
Dis[j] = min(Dis[u], MAP[u][j]);
}
}
}
}
```