Baidu shortest 2 Star preliminaries 3

Shortest 2

If you look at the meaning of the title of this topic is very simple, but I was out of a very nasty bug, look for half an hour.

Title meant to give you a map, find the shortest path, and then ask you if I asked the shortest path between two points, the worst to first to enumerate a few points.

For example, 1-4-2-5 I asked shortest 1-5, is not to enumerate to 4 in order to come out before this point.

Then we make the d [1] [5] = 4   

Because this n, m are relatively small, so you can run the shortest path between n times, then ran every time to record what the maximum number of points before the point.

My bug in reading, reading when I read the first n edges, then continue runtime error should be read m edges

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 998244353;
typedef long long ll;
ll d[maxn], num[maxn];
int n, m;
bool vis[maxn];
struct edge {
    ll from, to, dist, nxt;
    edge(ll from = 0, ll to = 0, ll dist = 0, ll nxt = 0) :from(from), to(to), dist(dist), nxt(nxt) {}
}ex[maxn];
struct heapnode {
    ll d, u;
    heapnode(ll d = 0, ll u = 0) : d(d), u(u) {}
    bool operator<(const heapnode &a) const {
        return a.d < d;
    }
};

ll head[maxn], cnt = 0;

void init() {
    memset(head, -1, sizeof(head));
    cnt = 0;
}

void add(ll u, ll v, ll w) {
    ex[cnt] = edge(u, v, w, head[u]);
    head[u] = cnt++;
    ex[cnt] = edge(v, u, w, head[v]);
    head[v] = cnt++;
}

void dijkstra(int s) {
    priority_queue<heapnode>que;
    while (!que.empty()) que.pop();
    memset(d, inf64, sizeof(d));
    d[s] = 0, num[s] = 0;
    memset(vis, 0, sizeof(vis));
    que.push(heapnode(0, s));
    while (!que.empty()) {
        heapnode x = que.top(); que.pop();
        ll u = x.u;
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; i != -1; i = ex[i].nxt) {
            edge &e = ex[i];

            if (d[e.to] == d[u] + e.dist) num[e.to] = min(num[e.to], max(num[u], u));
            if (d[e.to] > d[u] + e.dist) {
                d[e.to] = d[u] + e.dist;
                num[e.to] = max(num[u], u);
                if (u == s) num[e.to] = 0;
                que.push(heapnode(d[e.to], e.to));
            }
        }
    }
}


int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        init();
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i++) {
            ll u, v, w;
            scanf("%I64d%I64d%I64d", &u, &v, &w);
            add(u, v, w);
        }
        ll ans = 0;
        for (int i = 1; i <= n; i++) {
            dijkstra(i);
            for ( int j = 1 ; j <= n; j ++ ) { 
                years + = num [j] ;; 
                years % = mod; 
            } 
        } 
        Printf ( " % I64d \ n " , year); 
    } 
    Return  0 ; 
}
View Code

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/11405015.html