-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbellman.cpp
More file actions
43 lines (33 loc) · 818 Bytes
/
bellman.cpp
File metadata and controls
43 lines (33 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#define INF 0x3f3f3f3f3f
using namespace std;
const int N = 2100;
struct node//存边
{
int x,y,len;//表示x到y的距离是len
} a[N*2];
long long dis[N];//dis[i]表示从源点到i点的距离是dis[i]
int t,n;
void bellman()
{
for(int i = 1; i <= n; i++)//初始化
dis[i] = INF;
dis[1] = 0;
for(int i = 1; i < n; i++)//核心代码,n个点
for(int j = 1; j <= t; j++)//t条边
{
dis[a[j].y] = min(dis[a[j].x]+a[j].len, dis[a[j].y]);
dis[a[j].x] = min(dis[a[j].y]+a[j].len, dis[a[j].x]);//无向图双向考虑
}
}
int main()
{
while(cin>>t>>n)
{
for(int i = 1; i <= t; i++)//输入
cin>>a[i].x>>a[i].y>>a[i].len;
bellman();
cout<<dis[n]<<endl;
}
return 0;
}