题目:https://www.luogu.com.cn/problem/P4779
记录:https://www.luogu.com.cn/record/34933861
代码:
//邻接矩阵,得部分分
//V_MAX = 5600是极限值了
//#define LOCAL
#include <cstdio>
#include <cstdlib>
#define INF 0x7fffff
#define E_MAX 200
#define V_MAX 5600
int edges[V_MAX][V_MAX];
int costs[V_MAX];
int noticed[V_MAX];
int E;
int V;
void debug(){
for(int i = 0; i < V_MAX; i++){
for(int j = 0; j < V_MAX; j++){
printf("%d ", edges[i][j]);
}
printf("\n");
}
}
void readData(){
scanf("%d%d", &V, &E);
int tmp;
scanf("%d", &tmp);
for(int i = 0; i < E; i++){
int start;
int end;
int len;
scanf("%d%d%d", &start, &end, &len);
start -= 1;
end -= 1;
edges[start][end] = len;
}
//debug();
}
void initdijkstra(){
E = V = 0;
for(int i = 0; i < V_MAX; i++){
for(int j = 0; j < V_MAX; j++){
edges[i][j] = INF;
}
costs[i] = INF;
noticed[i] = 0;
}
costs[0] = 0;
}
int find_lowest(){
int value = INF;
int index = 0;
for(int i = 0; i < V; i++){
if(noticed[i] == 0 && costs[i] < value){
value = costs[i];
index = i;
}
}
return index;
}
void dijkstra(){
for(int i = 0; i < V - 1; i++){
int node = find_lowest();
for(int n = 0; n < V; n++){
if(edges[node][n] != INF && n != node){
int new_cost = costs[node] + edges[node][n];
if(costs[n] > new_cost){
costs[n] = new_cost;
}
}
}
noticed[node] = 1;
node = find_lowest();
}
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
initdijkstra();
readData();
dijkstra();
for(int i = 0; i < V; i++){
printf("%d ", costs[i]);
}
return 0;
}
你可以拿这个代码在在线IDE上面试一下,样例能过
但是题目评测就不能,会RE