PATA 1003 Emergency

insert image description here
insert image description here

Inscription

In this question, the point weight is added to the shortest distance of the most basic single-source node, and the number of shortest distance routes is calculated.
The key idea (how to find the number of shortest lines and call the most rescuers)
is to assume that the point with the shortest distance to the origin s is u, the node where u goes one step further is denoted as v, and the shortest distance of u is d[u], The number of its shortest routes is num[u], the maximum number of rescuers that can be reached at its location (with the shortest distance) is w[u], and v points are the same, G[u][v] records u to v At this time, the relevant information of u is already optimal
1. If d[u]+G[u][v]<d[v], it means that v can be better with the help of u, then d[v] =d[u]+G[u][v];
The number of shortest routes of v is covered as num[u], that is, num[v]=num[u], because the original saved is not the current optimal one, the original We don't need those routes; the number of rescuers that can be reached at this time is w[v]=w[u]+weight[v], because the shortest distance is the main, and the number of rescuers is the second condition. In the shorter case, this route should be used even if the number of rescuers is not the largest;
2. If d[u]+G[u][v]=d[v], then the shortest distance does not need to be updated at this time;
The number of shortest routes in v is equal to adding num[u] shortest routes to the number of existing shortest routes. All these shortest routes can be used; (note that it is not added by 1)
The number of rescuers needs to be judged at this time. If the number of rescuers in the original route is large or the number of rescuers that can be summoned by the route passing through u, the maximum value is taken; in
addition, it is necessary to pay attention to whether it is to find the shortest u from the current starting point s or start from u To update adjacent vertices, you must first determine whether the node has been visited.

code show as below

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int Maxv=500+10;
const int INF=1000000000;

//输入
//城市数量、边数、起点、终点
int n,m,st,ed;
//图、各城市点权
int G[Maxv][Maxv],weight[Maxv];
//最大点权,最短距离,最短距离路线数
int w[Maxv],d[Maxv],num[Maxv];
bool visit[Maxv]={
    
    false};

void dijstra(int s){
    
    //起点为s
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    fill(d,d+Maxv,INF);
    d[s]=0;
    num[s]=1;
    w[s]=weight[s];
    for(int i=0;i<n;i++){
    
    //一共n个顶点,循环n次
        int u=-1,MIN=INF;
        for(int j=0;j<n;j++){
    
    
            if(visit[j]==false&&d[j]<MIN){
    
    
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1) //剩下的结点和起点s不连通
            return;
        visit[u]=true;
        for(int v=0;v<n;v++){
    
    
            if(visit[v]==false&&G[u][v]!=INF){
    
    
                if(d[u]+G[u][v]<d[v]){
    
    //借助u可以使v的最短距离更优
                    d[v]=d[u]+G[u][v];
                    w[v]=w[u]+weight[v];
                    num[v]=num[u];
                }
                else if(d[u]+G[u][v]==d[v]){
    
    
                    if(w[u]+weight[v]>w[v]){
    
    
                        w[v]=w[u]+weight[v];
                    }
                    num[v]+=num[u];
                }
            }

        }
    }

}


int main()
{
    
    
    scanf("%d %d %d %d",&n,&m,&st,&ed);
    for(int i=0;i<n;i++)
        scanf("%d",&weight[i]);
    fill(G[0],G[0]+Maxv*Maxv,INF);
    int u,v;
    for(int i=0;i<m;i++){
    
    
        scanf("%d %d",&u,&v);
        scanf("%d",&G[u][v]);
        G[v][u]=G[u][v];
    }
    dijstra(st);
    printf("%d %d\n",num[ed],w[ed]);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346945&siteId=291194637