注意:题目中没有零权边,不然就可能会有无数条最短路。
目录
方法一:Dijkstra+堆优化
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
unordered_map<int,int> graph[MAXN];
int v_w[MAXN]; // 点权
int dis[MAXN],done[MAXN];
int path_num[MAXN],team_num[MAXN]; // 最短路条数和最大营救队数目
int n,m,c1,c2;
struct QNode{
int v,c;
bool operator< (const QNode &r) const {return c>r.c;}
QNode(int _v=-1,int _c=INF):v(_v),c(_c){}
};
void Dijkstra(int s)
{
memset(dis,INF,sizeof(dis));
memset(done,false,sizeof(done));
memset(path_num,0,sizeof(path_num));
memset(team_num,0,sizeof(team_num));
dis[s]=0;path_num[s]=1,team_num[s]=v_w[s];
priority_queue<QNode> q;
q.push(QNode(s,0));
while(!q.empty()){
int now=q.top().v;q.pop();
if(done[now]) continue;
done[now]=true;
for(auto edge:graph[now]){
int to=edge.first,cost=edge.second;
if(!done[to]&&dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
q.push(QNode(to,dis[to]));
path_num[to]=path_num[now];
team_num[to]=team_num[now]+v_w[to];
}else if(!done[to]&&dis[to]==dis[now]+cost){
path_num[to]+=path_num[now];
if(team_num[to]<team_num[now]+v_w[to])
team_num[to]=team_num[now]+v_w[to];
}
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;++i) cin>>v_w[i];
int x,y,z;
for(int i=0;i<m;++i){
cin>>x>>y>>z;
graph[x][y]=graph[y][x]=z;
}
Dijkstra(c1);
cout<<path_num[c2]<<" "<<team_num[c2];
}
方法二:Dijkstra+堆优化+DFS
适用于标尺特别多的情况,先把最短路径前驱树(根据距离标尺)记录下来。对这棵树进行 DFS 的时,再根据其他标尺进行判断。
int path_num[MAXN]
和 team_num[MAXN]
就不再需要了。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
unordered_map<int,int> graph[MAXN];
int v_w[MAXN]; // 点权
int dis[MAXN],done[MAXN];
set<int> pre[MAXN]; // 最短路前驱树
int n,m,c1,c2;
struct QNode{
int v,c;
bool operator< (const QNode &r) const {return c>r.c;}
QNode(int _v=-1,int _c=INF):v(_v),c(_c){}
};
void Dijkstra(int s)
{
memset(dis,INF,sizeof(dis));
memset(done,false,sizeof(done));
dis[s]=0;
priority_queue<QNode> q;
q.push(QNode(s,0));
while(!q.empty()){
int now=q.top().v;q.pop();
if(done[now]) continue;
done[now]=true;
for(auto edge:graph[now]){
int to=edge.first,cost=edge.second;
if(!done[to]&&dis[to]>=dis[now]+cost){
if(dis[to]!=dis[now]+cost)
pre[to].clear();
pre[to].insert(now);
dis[to]=dis[now]+cost;
q.push(QNode(to,dis[to]));
}
}
}
}
int max_team=0,roads=0;
void DFS(int s,int team)
{
team+=v_w[s];
if(pre[s].empty()){
roads++;
if(team>max_team) max_team=team;
}else{
for(auto i:pre[s]){
DFS(i,team);
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;++i) cin>>v_w[i];
int x,y,z;
for(int i=0;i<m;++i){
cin>>x>>y>>z;
graph[x][y]=graph[y][x]=z;
}
Dijkstra(c1);
DFS(c2,0);
cout<<roads<<" "<<max_team;
}
方法三:BellmanFord
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
int n,m,c1,c2;
struct Edge{
int u,v,c;
Edge(int _u=-1,int _v=-1,int _c=INF):u(_u),v(_v),c(_c){}
};
int v_w[MAXN];
vector<Edge> edges;
int dis[MAXN];
set<int> pre[MAXN];
int path_num[MAXN],team_num[MAXN];
void BellmanFord(int s)
{
memset(dis,INF,sizeof(dis));
memset(path_num,0,sizeof(path_num));
memset(team_num,0,sizeof(team_num));
dis[s]=0;path_num[s]=1,team_num[s]=v_w[s];
for(int i=0;i<n-1;++i){
for(auto e:edges){
if(dis[e.v]>dis[e.u]+e.c){
dis[e.v]=dis[e.u]+e.c;
pre[e.v].clear();pre[e.v].insert(e.u);
path_num[e.v]=path_num[e.u];
team_num[e.v]=team_num[e.u]+v_w[e.v];
}else if(dis[e.v]==dis[e.u]+e.c){
// 不同于 Dijkstra,由于有重复的松弛操作,
// 所以这里要重新统计最短路径。
pre[e.v].insert(e.u);
path_num[e.v]=0;
for(auto i:pre[e.v])
path_num[e.v]+=path_num[i];
if(team_num[e.v]<team_num[e.u]+v_w[e.v])
team_num[e.v]=team_num[e.u]+v_w[e.v];
}
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
int x,y,z;
for(int i=0;i<n;++i) cin>>v_w[i];
for(int i=0;i<m;++i){
cin>>x>>y>>z;
edges.emplace_back(x,y,z);
edges.emplace_back(y,x,z);
}
BellmanFord(c1);
cout<<path_num[c2]<<" "<<team_num[c2];
}
方法四:BellmanFord+DFS
方法三里面前驱子图都求出来了,那么对于其他标尺的DFS就按照方法二写一个函数就完事儿 , 就不给代码啦 ~
int path_num[MAXN]
和 team_num[MAXN]
也不再需要了,函数内部简化不少。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
int n,m,c1,c2;
struct Edge{
int u,v,c;
Edge(int _u=-1,int _v=-1,int _c=INF):u(_u),v(_v),c(_c){}
};
vector<Edge> edges;
int v_w[MAXN];
int dis[MAXN];
set<int> pre[MAXN];
void BellmanFord(int s)
{
memset(dis,INF,sizeof(dis));
dis[s]=0;
for(int i=0;i<n-1;++i){
for(auto e:edges){
if(dis[e.v]>=dis[e.u]+e.c){
if(dis[e.v]!=dis[e.u]+e.c)
pre[e.v].clear();
dis[e.v]=dis[e.u]+e.c;
pre[e.v].insert(e.u);
}
}
}
}
int max_team=0,roads=0;
void DFS(int s,int team)
{
team+=v_w[s];
if(pre[s].empty()){
roads++;
if(team>max_team) max_team=team;
}else{
for(auto i:pre[s]){
DFS(i,team);
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
int x,y,z;
for(int i=0;i<n;++i) cin>>v_w[i];
for(int i=0;i<m;++i){
cin>>x>>y>>z;
edges.emplace_back(x,y,z);
edges.emplace_back(y,x,z);
}
BellmanFord(c1);
DFS(c2,0);
cout<<roads<<" "<<max_team;
}
方法五:SPFA
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
int n,m,c1,c2;
unordered_map<int,int> graph[MAXN];
int v_w[MAXN];
int dis[MAXN],in_q[MAXN];
set<int> pre[MAXN];
int path_num[MAXN],team_num[MAXN];
void SPFA(int s)
{
memset(dis,INF,sizeof(dis));
memset(in_q,false,sizeof(in_q));
memset(path_num,0,sizeof(path_num));
memset(team_num,0,sizeof(team_num));
dis[s]=0;path_num[s]=1,team_num[s]=v_w[s];
queue<int> q;
q.push(s),in_q[s]=true;
while(!q.empty()){
int now=q.front();q.pop(),in_q[now]=false;
for(auto edge:graph[now]){
int to=edge.first,cost=edge.second;
if(dis[to]>dis[now]+cost){
dis[to]=dis[now]+cost;
if(!in_q[to]) q.push(to),in_q[to]=true;
pre[to].clear();pre[to].insert(now);
path_num[to]=path_num[now];
team_num[to]=team_num[now]+v_w[to];
}else if(dis[to]==dis[now]+cost){
pre[to].insert(now);path_num[to]=0;
for(auto i:pre[to])
path_num[to]+=path_num[i];
// 注意 这里更新了 num 之后要把 to 入队。
if(!in_q[to]) q.push(to),in_q[to]=true;
if(team_num[to]<team_num[now]+v_w[to])
team_num[to]=team_num[now]+v_w[to];
}
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;++i) cin>>v_w[i];
int x,y,z;
for(int i=0;i<m;++i){
cin>>x>>y>>z;
graph[x][y]=graph[y][x]=z;
}
SPFA(c1);
cout<<path_num[c2]<<" "<<team_num[c2];
}
方法六:SPFA+DFS
方法五里面前驱子图 pre 也顺带求出来啦,剩下的就同方法四了~
int path_num[MAXN]
和 team_num[MAXN]
也不再需要了。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
int n,m,c1,c2;
unordered_map<int,int> graph[MAXN];
int v_w[MAXN];
int dis[MAXN],in_q[MAXN];
set<int> pre[MAXN];
void SPFA(int s)
{
memset(dis,INF,sizeof(dis));
memset(in_q,false,sizeof(in_q));
dis[s]=0;
queue<int> q;
q.push(s),in_q[s]=true;
while(!q.empty()){
int now=q.front();
q.pop(),in_q[now]=false;
for(auto edge:graph[now]){
int to=edge.first,cost=edge.second;
if(dis[to]>=dis[now]+cost){
if(dis[to]!=dis[now]+cost)
pre[to].clear();
pre[to].insert(now);
dis[to]=dis[now]+cost;
if(!in_q[to]) q.push(to),in_q[to]=true;
}
}
}
}
int max_team=0,roads=0;
void DFS(int s,int team)
{
team+=v_w[s];
if(pre[s].empty()){
roads++;
if(team>max_team) max_team=team;
}else{
for(auto i:pre[s]){
DFS(i,team);
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;++i) cin>>v_w[i];
int x,y,z;
for(int i=0;i<m;++i){
cin>>x>>y>>z;
graph[x][y]=graph[y][x]=z;
}
SPFA(c1);
DFS(c2,0);
cout<<roads<<" "<<max_team;
}
方法七:DFS直接搜索
没错,这道题规模很小,直接搜索也是可以的。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=510;
const int INF=0x3f3f3f3f;
int n,m,c1,c2;
unordered_map<int,int> graph[MAXN];
int v_w[MAXN];
int min_dis=INF,path_num=0,team_num=0,vis[MAXN];
void DFS(int now,int dis_now,int team_now)
{
if(dis_now>min_dis) return;
if(now==c2){
if(dis_now<min_dis){
min_dis=dis_now;
path_num=1;
team_num=team_now;
}else if(dis_now==min_dis){
path_num++;
if(team_num<team_now)
team_num=team_now;
}
return;
}
for(auto edge:graph[now]){
int to=edge.first,cost=edge.second;
if(!vis[to]&&dis_now+cost<=min_dis){
vis[to]=true;
DFS(to,dis_now+cost,team_now+v_w[to]);
vis[to]=false;
}
}
}
int main() {
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;++i) cin>>v_w[i];
int x,y,z;
for(int i=0;i<m;++i){
cin>>x>>y>>z;
graph[x][y]=graph[y][x]=z;
}
memset(vis,false,sizeof(vis));
vis[c1]=true;
DFS(c1,0,v_w[c1]);
cout<<path_num<<" "<<team_num;
}