road watch

Topic description

Z-Kingdom has modern transportation extending in all directions. It was the time of independence celebrations, and with the increasing number of tourists from neighboring countries, crime also began to grow quietly.

Police from the Special Mission Support Section received a request from headquarters to investigate criminals disguised as tourists. Through their investigation, they obtained a map that recorded the length of every road within the Z-Kingdom.

Obviously, in order to reduce the possibility of criminal behavior being detected, criminals will always choose the shortest path to act. To facilitate staffing and to speculate on the routes taken by criminals, they want to know how many paths criminals might take between any two locations.

Input and output format

Input format:

The first line contains two integers N, M, which represent the number of places and roads in the Z-Kingdom.


Next N lines, each line contains three integers x, y, z, indicating the labels of two different places connected by the road, and the length of the road. Roads are two-way.


There will never be more than one road between two different locations.


Output format:

Output a line containing N (N-1)/2 integers .


where represents how many roads a criminal might take between location x and location y.


Input and output example




Input Example #1:
Copy
5 6
1 2 1
2 3 1
3 4 1
4 1 1
2 4 2
4 5 4



Output Sample #1:
Copy
1 4 1 2 1 5 6 1 2 1



illustrate


【Data scale】


For 30% of the data, ensure N≤50


For 60% of the data, N≤100 is guaranteed


For 100% of the data, N≤500 is guaranteed.


【Time and space limitations】


5s/128M

answer

First use Floyed to find the shortest distance between points.
(The following uses \(f_{x,y}\) to represent the shortest distance from \(x\) to \(y\) , \(a_{x,y}\) represents the edge connecting \(x,y\) length)
enumerate \(x\) , then enumerate \(y,z\) , if \(f_{x,y}+a_{y,z}=f_{x,z}\) , then The shortest path from \(x\) to \(z\) must cover the edge of \(a_{x,y}\) , so the \(++S[z]\)
\(S\) array is processed Then enumerate \(y,z\) \(f_{x,y}+f_{y,z}=f_{x,z}\) to illustrate the shortest path from \(x\) to \(z\) After \(y\) , there are \(s[y]\) edges, so \(ans[x][y]=\sum{s[k]}\)

#include<bits/stdc++.h>
#define gc getchar
#define ll long long
inline ll read(){ll x = 0; char ch = gc(); bool positive = 1;for (; !isdigit(ch); 
ch = gc()) if (ch == '-')  positive = 0;for (; isdigit(ch); ch = gc())  x = x * 10 
+ ch - '0';return positive ? x : -x;}inline void write(ll a){if(a>=10)write(a/10);
putchar('0'+a%10);}inline void writeln(ll a){if(a<0){a=-a; putchar('-');}write(a);
puts("");}
using namespace std;
const int N = 510, M = 500;
int n, m;
int f[N][N], s[N], ans[N][N], a[N][N];
int main() {
    int x, y;
    n = read(), m = read();
    memset(f, 0x3f, sizeof f);
    for(int i = 1; i <= n; ++i) f[i][i] = 0;
    for(int i = 1; i <= m; ++i) {
        x = read(), y = read();
        a[x][y] = a[y][x] = f[x][y] = f[y][x] = read();
    }
    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
            for(int j = 1;j <= n; ++j)
                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
    for(int i = 1; i <= n; ++i) {
        memset(s, 0, sizeof s);
        for(int j = 1; j <= n; ++j)
            if(i != j)
                for(int k = 1; k <= n; ++k)
                    if(a[j][k])
                        if(f[i][j] == f[i][k] + a[k][j])
                            ++s[j];
        for(int j = 1; j <= n; ++j)
            if(i != j)
                for(int k = 1; k <= n; ++k)
                    if(f[i][j] == f[i][k] + f[k][j])
                        ans[i][j] += s[k];
    }
    for(int i = 1; i <= n; ++i)
        for(int j = i + 1; j <= n; ++j)
            printf("%d ", ans[i][j]);
    return 0;
}

Guess you like

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