当前位置:网站首页>Luogu p1608 path statistics solution

Luogu p1608 path statistics solution

2022-06-21 22:08:00 q779

Luogu P1608 Path Statistics Answer key

Topic link :P1608 Path Statistics

The question

One sentence question : Shortest circuit count .

“RP The restaurant ” The quality of the staff is not average , After calculating the same phone number in unison , Ready to let HZH,TZY To deliver fast food , They drew a map of the city where they lived , Known on their maps , Yes N N N A place to , And they are currently in a position marked “1” In a small town , The place of delivery is marked as “N” The town of .( It's a bit of rubbish ) Besides, I also know that these roads are one-way , From the town I I I To J J J Need to spend D [ I , J ] D[I, J] D[I,J] Time for , In order to deliver fast food to customers more efficiently and quickly , They want to take a route from the town 1 1 1 To the town N N N The least expensive way , But before they set out , Hit someone who was angry because of the traffic jam on the road FYY, Inspired by , You can't just know one route , In case ... therefore , They invited FYY Let's study the next question : How many paths are there that cost the least ?

about 100 % 100\% 100% The data of 1 ≤ N ≤ 2000 1\leq N\leq 2000 1N2000, 0 ≤ E ≤ N × ( N − 1 ) 0\leq E\leq N\times (N-1) 0EN×(N1), 1 ≤ C ≤ 10 1\leq C\leq 10 1C10.

The data of the shortest path meter says that it cannot be used spfa(spfa Is dead

set up f u f_u fu From the beginning to the beginning u u u The shortest path number of nodes

Obviously with dijkstra Brush Watch

if ( u , v ) (u,v) (u,v) Relaxation succeeded , That is to say

d[v]>d[u]+w

Let's just let f[v]=f[u]

Otherwise, if

d[v]==d[u]+w

let f[v]+=f[u]

be without , That's the water

Time complexity O ( n log ⁡ m ) O(n \log m) O(nlogm) , Here I write the adjacency matrix so m ≈ n 2 m \approx n^2 mn2

Code :

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(2e3+15)

int n,m,d[N],vis[N],f[N],g[N][N];
struct node
{
    
    int u,dis;
    bool operator<(const node &o)const
        {
    return dis>o.dis;}
};
priority_queue<node> q;
void dijkstra(int st)
{
    
    memset(d,0x3f,sizeof(d));
    d[st]=0;f[st]=1;
    q.push({
    st,0});
    while(!q.empty())
    {
    
        int u=q.top().u;q.pop();
        if(vis[u])continue;
        vis[u]=1;
        for(int v=1,w; v<=n; v++)
        {
    
            if((w=g[u][v])>=INF)continue;
            if(d[v]>d[u]+w)
            {
    
                d[v]=d[u]+w;
                f[v]=f[u];
                q.push({
    v,d[v]});
            }else if(d[v]==d[u]+w)f[v]+=f[u];
        }
    }
}   
signed main()
{
    
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            g[i][j]=INF;
    for(int i=1,u,v,w; i<=m; i++)
    {
    
        cin >> u >> v >> w;
        g[u][v]=min(g[u][v],w);
    }
    
    dijkstra(1);
    if(d[n]>=INF)cout << "No answer\n";
    else cout << d[n] << " " << f[n];
    return 0;
}

Reprint please explain the source

原网站

版权声明
本文为[q779]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212013313693.html