L2-001. 紧急救援 dijkstra,适合模版

https://www.patest.cn/contests/gplt/L2-001

dijkstra,在求最短路的基础上增加了权值,权值越大越好。

#include 
using namespace std;

const int maxn=505;
const int INF=0x3f3f3f3f;

int n,m,st,ed;
int G[maxn][maxn]; //城市与城市之间的距离 
int Val[maxn];   //每个城市的救援队数量
int dis[maxn];   //距离起点的最短距离 
int w[maxn];    //距离起点能获得的最多救援队
int num[maxn];  //从起点出发有几条路能达到
bool vis[maxn];
int pre[maxn];

void Dijkstra(){

  dis[st]=0;
  w[st]=Val[st];
  num[st]=1;
  for (int i=0;iint u=-1;
    int min_dis=INF;
    for (int j=0;jif (!vis[j]&&dis[j]if (u==-1) break;
    vis[u]=1;
    for (int v=0;vif (!vis[v]&&G[u][v]!=INF){
        if (dis[v]>dis[u]+G[u][v]){
          dis[v]=dis[u]+G[u][v];
          num[v]=num[u];
          w[v]=Val[v]+w[u];
          pre[v]=u;
        }
        else
        if (dis[v]==dis[u]+G[u][v]){
          num[v]+=num[u];
          if (w[v]void printPath(int v){
  if (v==st){
    cout << v;
    return;
  }
  printPath(pre[v]);
  cout << " " << v;
}

int main(){

  cin >> n >> m >> st >> ed;

  fill(G[0],G[0]+maxn*maxn,INF);
  fill(dis,dis+maxn,INF);
  for (int i=0;icin >> Val[i];
  }
  for (int i=0;iint u,v,c;
    cin >> u >> v >> c;
    G[u][v]=c;
    G[v][u]=c;
  }

  Dijkstra();

  cout << num[ed] << " " << w[ed] << endl;

  printPath(ed);
} 

你可能感兴趣的:(最短路)