# 最短路

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 80757    Accepted Submission(s): 34969

Problem Description

Input

Output

Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0

Sample Output
3 2

• 不知道什么名字的算法：

 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author 24454955 2018-04-16 21:04:36 Accepted 2544 62MS 2024K 808 B C++ zqiangda

```#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
struct Node{
int s, e, w;
Node(int a, int b, int c) :s(a), e(b), w(c){}
};
int main()
{
int n, m;
long len[105];
vector<Node>vec;
while (cin >> n >> m, n+m){
vec.clear();
for (int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
vec.push_back(Node(a, b, c));
}
memset(len, 125, sizeof(len));
len[1] = 0;

int flag = 1;
while (flag){
flag = 0;
for (int i = 0; i < vec.size(); i++){
int s = vec[i].s, e = vec[i].e, w = vec[i].w;

if (len[e]>len[s] + w){
len[e] = len[s] + w;
flag = 1;
}
if (len[s] > len[e] + w){
len[s] = len[e] + w;
flag = 1;
}
}
}
cout << len[n] << endl;
}
return 0;
}```

•  SPFA算法：
 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author 24455250 2018-04-16 21:19:34 Accepted 2544 62MS 2068K 974 B C++ zqiangda
```#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct Node{
int e, w;
Node(int b, int c) :e(b), w(c){}
};
int n, m;
long len[105], vis[105];
vector<Node>vec[105];
void spfa(int s)
{
memset(len, 125, sizeof(len));
memset(vis, 0, sizeof(vis));
len[s] = 0;
queue<int>que;
que.push(s);
while (!que.empty()){
int p = que.front(); que.pop();
vis[p] = 0;
for (int i = 0; i < vec[p].size(); i++){
int e = vec[p][i].e, w = vec[p][i].w;
if (len[e]>len[p] + w){
len[e] = len[p] + w;
if (!vis[e]){
que.push(e);
vis[e] = 1;
}
}

}
}

}
int main()
{

while (cin >> n >> m, n+m){
for (int i = 1; i <= n;i++)
vec[i].clear();
for (int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
vec[a].push_back(Node(b, c));
vec[b].push_back(Node(a, c));
}

spfa(1);
cout << len[n] << endl;
}
return 0;
}```
• dijkstra算法：
 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author 24455452 2018-04-16 21:32:54 Accepted 2544 46MS 2064K 1123 B C++ zqiangda
```#include<cstdio>
#include<vector>
#include<cstring>
#include<climits>
#include<iostream>
using namespace std;
struct Node{
int e, w;
Node(int b, int c) :e(b), w(c){}
};
int n, m;
long len[105], vis[105];
vector<Node>vec[105];
void dijkstra(int s)
{
memset(len, 125, sizeof(len));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < vec[s].size(); i++){
int e = vec[s][i].e, w = vec[s][i].w;
len[e] = w;
}
len[s] = 0; vis[s] = 1;

for (int i = 0; i < n; i++){
int min = INT_MAX, p = 1;
for (int j = 1; j <= n; j++){
if (len[j] < min&&vis[j] == 0){
min = len[j]; p = j;
}
}

vis[p] = 1;

for (int j = 0; j < vec[p].size(); j++){
int e = vec[p][j].e, w = vec[p][j].w;
if (len[e]>len[p] + w&&vis[e] == 0){
len[e] = len[p] + w;
}
}
}
}
int main()
{

while (cin >> n >> m, n+m){
for (int i = 1; i <= n;i++)
vec[i].clear();
for (int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
vec[a].push_back(Node(b, c));
vec[b].push_back(Node(a, c));
}
dijkstra(1);
cout << len[n] << endl;
}
return 0;
}```

• Floyd算法：
```#include<cstdio>
#include<vector>
#include<cstring>
#include<climits>
#include<iostream>
using namespace std;
int main()
{
int n, m;
long map[105][105];
while (cin >> n >> m, n+m){
//memset(map, 125, sizeof(map));
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
map[i][j] = INT_MAX;
map[i][i] = 0;
}
for (int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
map[a][b] = map[b][a] = c;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++){
if (map[i][k] + map[k][j] < map[i][j])
map[i][j] = map[i][k] + map[k][j];
//    printf("%10d\t", map[i][j]);
}
cout << map[1][n] << endl;
}
return 0;
}```

hdu 2544 最短路

(0)
(0)

0条