1003-emergency

经验教训:

  1. Dijstra算法中通过设置bool类型数组标志一个顶点是否被包含
  2. 算法中设置prev可以用来回溯
  3. 这里的cnt计算最短路径的条数的方法很好,大大缩短了代码运行时间
  4. 顶点权值的相加—第一次用,要掌握
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <vector>
#include <climits>
//#define MAX 500
using namespace std;
typedef struct node
{
int id;
int weight;
}node;
typedef struct edge
{
int from ;
int to;
int weight;
}edge;
void Dijstra(int home ,int target , int numCity , edge **road ,node *n)
{
bool *s = new bool[numCity];
int *dist = new int[numCity];
int *prev = new int[numCity]; //记录前驱
int *cnt =new int[numCity] ; //计数器
int *cap =new int[numCity*numCity];
//memset(cap , 0 , sizeof(cap));
int k = 0, i , j;
for(i = 0 ; i < numCity * numCity ; ++i)
cap[i] = 0;
cap[home] = n[home].weight;
for(i = 0 ; i < numCity ; ++i)
{
cnt[i] =1;
dist[i] = road[home][i].weight;
s[i] = false;
if(dist[i] == INT_MAX)
prev[i] = -1;
else
{
prev[i] = home;
cap[i] = n[home].weight+n[i].weight;
}
}
dist[home] = 0;
s[home] = true;
for(i = 0 ; i < numCity ; ++i)
{
int cost = n[target].weight;
int mindist = INT_MAX;
int u = home;
for(j = 0 ; j < numCity ; ++j)
{
if((!s[j]) && dist[j] < mindist)
{
u = j;
mindist = dist[j];
}
}
s[u] = true;
if(u == target)
break;
for(j = 0 ; j < numCity ; ++j)
{
if((!s[j]) && road[u][j].weight < INT_MAX)
{
if(dist[u] + road[u][j].weight < dist[j])
{
dist[j] = dist[u] + road[u][j].weight;
prev[j] = u;
cap[j] = cap[u] + n[j].weight;
cnt[j] = cnt[u];
}
else if(dist[u] + road[u][j].weight == dist[j])
{
if(cap[u] + n[j].weight > cap[j])
{
cap[j] = cap[u] + n[j].weight;
}
cnt[j] += cnt[u];
}
}
}
}
printf("%d %d\n" , cnt[target] , cap[target]);
delete[] s;
delete[] dist;
delete[] prev;
delete[] cnt;
delete[] cap;
delete[] n;
}
int main()
{
int numCity , numRoad , home , target;
scanf("%d %d %d %d" , &numCity , &numRoad , &home , &target);
int i , j;
node *n = new node[numCity];
for(i = 0 ; i < numCity ; ++i)
{
n[i].id = i;
// cin>>n[i].weight;
scanf("%d" , &n[i].weight);
}
//dynamic array build
edge **road;
road = new edge*[numCity];
for(i = 0 ; i < numCity ; ++i)
{
road[i] = new edge[numCity];
}
for(i = 0 ; i < numCity ; ++i)
{
for(j = 0 ; j < numCity ; ++j)
{
road[i][j].weight = INT_MAX;
}
}
for(i = 0 ; i < numRoad ; ++i)
{
int from , to;
scanf("%d %d" , &from , &to);
scanf("%d" , &road[from][to].weight);
road[to][from].weight = road[from][to].weight;
}
Dijstra(home , target , numCity , road , n);
return 0;
}

邻接矩阵版:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <algorithm>
#include <vector>
#define MAX 510
#define INF 1000000000
using namespace std;
int n , m;
int weight[MAX];
struct node
{
int id , dis;
};
vector<node> G[MAX];
bool vis[MAX] = {false};
int d[MAX] , w[MAX] = {0};
int path[MAX] = {0};//用来计数
void Dijstra(int s, int target)
{
fill(d , d+MAX , INF);
fill(path , path+MAX , 0);
fill(w , w+MAX , 0);//为了确保,还是再刷一次比较好
d[s] = 0;//保证先更新各个顶点到源点的距离
path[s] = 1;
w[s] = weight[s];
int i , j ;
for(i = 0 ; i < n ; ++i)
{
int u = -1 , min= INF;
for(j = 0 ; j < n ; ++j)
{
if(vis[j] == false && min > d[j])
{
u = j;
min = d[j];
}
}
if(u == -1)
return ;
vis[u] = true;
if(u == target)
{
//cout<<w[u] + weight[0]<<" "<<path[u]<<endl;
cout<<path[u]<<" "<<w[u]<<endl;
break;
}
for(j = 0 ; j < G[u].size() ; ++j)
{
int v = G[u][j].id;
if(vis[v] == false)
{
if(d[u] + G[u][j].dis < d[v])
{
d[v] = d[u] + G[u][j].dis;
w[v] = w[u] + weight[v];//点权跟进
path[v] = path[u];
}
else if(d[u] + G[u][j].dis == d[v] )//使用第二条件筛选
{
if(w[u] + weight[v] > w[v])
w[v] = w[u] + weight[v];
path[v] += path[u]; //在原来的基础上+(将父节点的路径数继承下来)
}
}
}
/*for(int x = 0 ; x < n ; ++x)
cout<<d[x]<<" ";
cout<<endl;*/
}
}
int main()
{
int c1 , c2;
cin>>n>>m>>c1>>c2;
int i;
for(i = 0 ; i < n ; ++i)
{
cin>>weight[i];
}
int a , b , dis;
node city;
for(i = 0 ; i < m ; ++i)
{
cin>>a>>b>>dis;
city.id = b;
city.dis = dis;
G[a].push_back(city);
city.id = a;
G[b].push_back(city);
}
Dijstra(c1 , c2);
return 0;
}

热评文章