|
|
|
|
|
|
|
|
|
回复 @丿Nice丶蒙奇 :
对于这个条件dis[i] > dis[x] + map[x][i]等于的时候也要更新,就可以克服邻接表顺序问题了的
|
|
VIP有没有用邻接表过的?我觉得用邻接表没办法确定顺序
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 110
#define M 10100
#define qm 110
#define INF 0x3f3f3f3f
int path[N][N] = {0};
int n,m,st;
int q[qm + 1];
int dis[N];
bool v[N];
int map[N][N];
void spfa(int s){
for (int i = 0; i < n; i++){
if (map[s][i] != INF){
path[i][0] = 2;
path[i][1] = s;
path[i][2] = i;
}
else path[i][0] = 0;
}
path[s][0]--;
memset(v,0,sizeof(v));
memset(dis,0x3f,sizeof(dis));
dis[s] = 0;
int h = 0,t = 0;
q[++t] = s;
while (h != t){
int x = q[h = (h % qm) + 1];
v[x] = 0;
for (int i = 0; i < n; i++){
if (map[x][i] != INF){
if (dis[i] > dis[x] + map[x][i]){
dis[i] = dis[x] + map[x][i];
for (int j = 1; j <= path[x][0]; j++){
path[i][j] = path[x][j];
}
path[i][0] = path[x][0];
path[i][++path[i][0]] = i;
if (!v[i]){
t = (t % qm) + 1;
q[t] = i;
v[i] = 1;
}
}
}
}
}
return;
}
int main(){
freopen("djs.in","r",stdin);
freopen("djs.out","w",stdout);
memset(map,0x3f,sizeof(map));
scanf("%d%d%d",&n,&m,&st);
int x,y,z;
for (int i = 1; i <= m; i++){
scanf("%d%d%d",&x,&y,&z);
map[x][y] = z;
}
for (int i = 0; i < n; i ++) map[i][i] = 0;
spfa(st);
for (int i = 0; i < n; i++){
printf("%d:\n",i);
if (dis[i] != INF && i != st){
printf("path:");
for (int j = 1; j <= path[i][0]; j++){
printf("%d ",path[i][j]);
}
puts("");
printf("cost:%d\n",dis[i]);
}else{
puts("no");
}
}
return 0;
}
|
|
|
|
|
|
本题有多解
比如第四个点 从1到0的最短距离是2 可以使从0-》2》1 也 可以 0-》9》1
考试前被这道题坑死了。。。 (算增加rp吧)
坑人啊啊啊啊啊!!!...
|
|
|
|
杀鸡可用宰牛刀:spfa足以秒杀此题,目前只是为了复习一下spfa。
|
|
|
|
|
|
|
|
OH YEAH!
一遍过!!
某年NOIP,CAR的旅行路线简单化+PATH路径随松驰更新=AC。
48行。
哦,对了,这题的路是有向的,无向是错误的……
|
|
|
|
|