第一次写这道题的时候,板子记忆不熟,错误频出,在此做出标注
ac代码: (代码好丑,求别喷emm)
#include<bits/stdc++.h>
#define LL long long
#define fo(i,a,b) for(int i=a;i<b;i++)
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
using namespace std;
const int maxn=505;
const int inf=1e9;
int G[maxn][maxn],d[maxn],pw[maxn];//邻接矩阵,最短距离,点权
int w[maxn],vis[maxn],num[maxn];//队伍总数,点访问情况,最短路径数
int n,m,st,ed;
void Dijkstra(int s){
d[s]=0;//原点到原点距离为0
w[s]=pw[s];//原点队伍总数=点权
num[s]=1;//注意:原点到原点最短路径数=1
fo(i,0,n){
int u=-1,MIN=inf;
fo(j,0,n)//
// if(vis[j]==0&&G[s][i]!=inf)注意:错误写法1
// if(vis[j]==0&&d[j]<inf)注意:错误写法2
if(vis[j]==0&&d[j]<MIN){
u=j;MIN=d[j];
}
if(u==-1)return;
vis[u]=true;//易忘
fo(v,0,n){
if(vis[v]==0&&G[u][v]!=inf){
if(d[v]>d[u]+G[u][v]){
d[v]=d[u]+G[u][v];
w[v]=w[u]+pw[v];
num[v]=num[u];
}//三重覆盖
// else if(d[v]=d[u]+G[u][v])注意:错误写法
else if(d[v]==d[u]+G[u][v]){
if(w[u]+pw[v]>w[v])
w[v]=w[u]+pw[v];
num[v]+=num[u];//易忘
}
}
}
}
}
int main(){
fill(G[0],G[0]+maxn*maxn,inf);
fill(d,d+maxn,inf);
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&n,&m,&st,&ed);
fo(i,0,n)scanf("%d",&pw[i]);//from 0 to n-1
int a,b,c;
fo(i,0,m){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=c;
G[b][a]=c;//无向边
}
Dijkstra(st);
printf("%d %d\n",num[ed],w[ed]);
return 0;
}
去掉标注后:
#include<bits/stdc++.h>
#define LL long long
#define fo(i,a,b) for(int i=a;i<b;i++)
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
using namespace std;
const int maxn=505;
const int inf=1e9;
int G[maxn][maxn],d[maxn],pw[maxn];//邻接矩阵,最短距离,点权
int w[maxn],vis[maxn],num[maxn];//队伍总数,点访问情况,最短路径数
int n,m,st,ed;
void Dijkstra(int s){
d[s]=0;//原点到原点距离为0
w[s]=pw[s];//原点队伍总数=点权
num[s]=1;//原点到原点最短路径数=1
fo(i,0,n){
int u=-1,MIN=inf;
fo(j,0,n)//
if(vis[j]==0&&d[j]<MIN){
u=j;MIN=d[j];
}
if(u==-1)return;
vis[u]=true;
fo(v,0,n){
if(vis[v]==0&&G[u][v]!=inf){
if(d[v]>d[u]+G[u][v]){
d[v]=d[u]+G[u][v];
w[v]=w[u]+pw[v];
num[v]=num[u];
}
else if(d[v]==d[u]+G[u][v]){
if(w[u]+pw[v]>w[v])
w[v]=w[u]+pw[v];
num[v]+=num[u];
}
}
}
}
}
int main(){
fill(G[0],G[0]+maxn*maxn,inf);
fill(d,d+maxn,inf);
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&n,&m,&st,&ed);
fo(i,0,n)scanf("%d",&pw[i]);//from 0 to n-1
int a,b,c;
fo(i,0,m){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=c;
G[b][a]=c;//无向边
}
Dijkstra(st);
printf("%d %d\n",num[ed],w[ed]);
return 0;
}