0%

HDU1874题:畅通工程续(Floyd,SPFA,迪杰斯特拉)

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874

畅通工程续

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 77717 Accepted Submission(s): 29941

Problem Description

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input

本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

1
2
3
4
5
6
7
8
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

1
2
2
-1

题解

Floyd算法

floyd是最好写的了,floyd是拿动态规划简化过来的算法(具体就不在博客里写了)

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
#include <iostream>
using namespace std;
const int maxn=210;

int dp[maxn][maxn];

int main(){
int n,m;
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i==j)
dp[i][j]=0;
else
dp[i][j]=(int)1e9;
int a,b,val;
for(int i=0;i<m;i++) {
cin >> a >> b >> val;
dp[a][b]=min(dp[a][b],val);
dp[b][a]=min(dp[b][a],val);
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
int s,t;
cin>>s>>t;
if(dp[s][t]==(int)1e9)
cout << -1 << endl;
else
cout << dp[s][t] << endl;
}
return 0;
}

三个for循环就是Floyd的核心代码了,不过话说拿这个了解DP真实挺好的了

SPFA算法

这是一个加上了松弛操作的BFS,基本写法跟BFS差不多,就多了一个加速的是否入队标记和d数组松弛操作

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=210;

struct edge{
int to,val;
edge(int to,int val){
this->to=to;
this->val=val;
}
};

vector<edge> g[maxn];
int d[maxn],inq[maxn];

int main(){
int n,m;
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=0;i<n;i++)
d[i]=(int)1e9,g[i].clear();
memset(inq,0,sizeof(inq));
int a,b,val;
for(int i=0;i<m;i++){
cin>>a>>b>>val;
g[a].emplace_back(edge(b,val));
g[b].emplace_back(edge(a,val));
}
int s,t;
cin>>s>>t;
queue<int> que;
d[s]=0;
inq[s]=1;
que.push(s);
while(!que.empty()){
int u=que.front();que.pop();
inq[u]=0;
for(auto it=g[u].begin();it!=g[u].end();it++){
int v=it->to;
if(d[v]>d[u]+it->val){
d[v]=d[u]+it->val;
if(inq[v]) continue;
que.push(v);
inq[v]=1;
}
}
}
if(d[t]==(int)1e9)
cout << -1 << endl;
else
cout << d[t] << endl;
}
return 0;
}

迪杰斯特拉算法

如果说上面的SPFA是以点为更新步骤的,那么迪杰斯特拉算法就是以边做更新的(此处diss一下Prim和Kruscal)

迪杰斯特拉使用了优先队列,放弃了SPFA的inq优化,所以我们得构造一个node并重写一下运算符,让边按权值从小到大排序,每次拿出最小的边做松弛就可以提高效率

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=210;

struct edge{
int to,val;
edge(int to,int val){
this->to=to;
this->val=val;
}
};

struct node{
int x,y;
bool operator < (const node& a) const{
return x>a.x;
}
node(int x,int y){
this->x=x;
this->y=y;
};
};

vector<edge> g[maxn];
int d[maxn];

int main(){
int n,m;
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=0;i<n;i++)
g[i].clear(),d[i]=(int)1e9;
int a,b,val;
for(int i=0;i<m;i++){
cin>>a>>b>>val;
g[a].emplace_back(edge(b,val));
g[b].emplace_back(edge(a,val));
}
int s,t;
cin>>s>>t;
priority_queue<node> que;
d[s]=0;
que.push(node(d[s],s));
while(!que.empty()){
int u=que.top().y;que.pop();
for(auto it=g[u].begin();it!=g[u].end();it++){
int v=it->to;
if(d[v]>d[u]+it->val){
d[v]=d[u]+it->val;
que.push(node(d[v],v));
}
}
}
if(d[t]==(int)1e9)
cout << -1 << endl;
else
cout << d[t] << endl;
}
return 0;
}