LINK
突然发现图论一窍不通……
现学现卖
题目:
思路:
纯纯dijkstra
这个讲的很好:
注意一个wa点:
7 9 0 6
0 0 2 3 8 5 0
0 1 1
1 2 1
1 3 1
2 4 1
3 5 1
4 6 1
5 6 1
2 5 1
3 4 1
此时路径数目应该是4
代码:
注释很详细了
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=500+10;
const int inf=0x3f3f3f3f;
int n,m,s,e;
int a[N];//每个节点队伍数目
bool vis[N];//标记已找过的点
int d[N][N];//邻接矩阵
int pre[N];//记录前面点的位置
int dis[N];//记录离出发点的距离
int num[N];//记录相同距离时路径数目
int maxx[N];//记录救援数目在路径最短时的最多数量
vector<int>v[N*2];//两点链表
void prin(int x){
if(x==s)return;
prin(pre[x]);
cout<<" "<<x;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>s>>e;
for(int i=0;i<n;i++)cin>>a[i],dis[i]=inf;
for(int i=0,x,y,z;i<m;i++){
cin>>x>>y>>z;
d[x][y]=z;
d[y][x]=z;
v[x].push_back(y);
v[y].push_back(x);
}
dis[s]=0;num[s]=1;maxx[s]=a[s];
for(int i=0;i<n;i++){
int t=-1;//记录此时最小的点
for(int j=0;j<n;j++){//找此时最小的点
if(!vis[j]&&(t==-1||(dis[t]>dis[j]))){
t=j;
}
}
vis[t]=1;//标记
// cout<<"t="<<t<<endl;
for(int j=0;j<v[t].size();j++){//遍历连接t的位置
int y=v[t][j];
if(vis[y])continue;
if(dis[y]>dis[t]+d[t][y]){//如果此时有更近的路径
dis[y]=dis[t]+d[t][y];
pre[y]=t;//更新前一个点
maxx[y]=maxx[t]+a[y];//更新此时的最大队伍数目
num[y]=num[t];//此点的路径数目
}else if(dis[y]==dis[t]+d[t][y]){//如果此时相等
num[y]+=num[t];//又多一种方案,num[t]条 注意这里!!!
if(maxx[t]+a[y]>maxx[y]){
maxx[y]=maxx[t]+a[y];
pre[y]=t;
}
}
// cout<<"num["<<y<<"]="<<num[y]<<" pre["<<y<<"]="<<pre[y]<<" maxx["<<y<<"]="<<maxx[y]<<endl;
}
// cout<<endl;
}
cout<<num[e]<<" "<<maxx[e]<<endl;
cout<<s;prin(e);
return 0;
}