2019 summer training camp shortest count

Title Description

Gives a N vertices undirected no right edges of the map M, the vertices numbered . 1 - N. Q from vertex 1 start, the shortest path to every other point there are several.

Input Format

The first row contains 2 positive integers N , M, the number of points and a top edge of FIG.

Next M rows, each row 2 positive integers y x , y, expressed a vertex x is connected to the apex edges y, note that there may be multiple edges from the ring.

Output Format

A total of N lines, each line a non-negative integer, the i i output line from the vertex 1 to vertex i how many different shortest, since the answer is likely to be very large, you only need to output A n- MOD 1 0 0 0 0 results after 3 to. If you can not reach the top i output 0.

Sample input and output

Input # 1
5 7
1 2
1 3
2 4
3 4
2 3
4 5
4 5
Output # 1
1
1
1
2
4

Description / Tips

1 to shortest path 5 are four, respectively, 2 1 - 2 - . 4 - 5 and 2 1 - . 3 - . 4 - 5 (due . 4 - sides 5 have two).

For 2 0 % of the data, N . 1 0 0;

For . 6 0 % of the data, N . 1 0 0 0;

For . 1 0 0 % of the data, N < = . 1 0 0 0 0 0 0 , M < = 2 0 0 0 0 0 0.

Source title Luo Gu P1144


Since it is sure to find the shortest count shortest d [i]

 

BFS then scan each point to a point which can reach, if u is v and d [v] == d [u] +1 answer is recorded
After careful not to repeat the same side rather than the point (not too heavy side),
Because if the record point whether this point i can be found to the number of points can only be found once, does not comply with the principle of addition
At the same time need not be considered loopback
Another point attention to ans [i] denotes the i th point of the final set point j can answer the point is if i meet
d [i] == d [j] +1 ans [i] + = ans [j] was added instead of 1 (as from point 1 to point j has come ANS [j] species, the multiplication principle can answer)
The Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define mod 100003
using namespace std;
int n,m,head[1000050],num,vst[4000050],book[4000050],d[1000050],ans[1000050];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
queue<int> q1;
struct edge
{
    int u,v,nxt;
}e[4000050];
void add(int u,int v)
{
    e[num].u=u;e[num].v=v;
    e[num].nxt=head[u];head[u]=num++;
}
int main()
{
    memset(head,-1,sizeof head);
    memset(d,127,sizeof d);
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        if(x!=y)
        {
            add(x,y);
            add(y,x);
        }
    }
    d[1]=0;
    q.push(make_pair(d[1],1));
    while(!q.empty())
    {
        int x=q.top().second;
        q.pop();
        if(vst[x])continue;
        vst[x]=. 1 ;
         for ( int ST = head [X]; ~ ST; ST = E [ST] .nxt) 
        { 
            int Y = E [ST] .v;
             IF (D [X] + . 1 < D [Y]) 
            { 
                D [Y] = D [X] + . 1 ; 
                q.push (the make_pair (D [Y], Y)); 
            } 
        } 
    } // Dijkstra shortest seek heap + 
    ANS [ . 1 ] = . 1 ; 
    q1.push ( . 1 ); // marking point 1 is only one way to be able to own (standing down) 
    the while (! q1.empty ()) 
    { 
        int= X q1.front (); 
        q1.pop (); 
        for ( int ST head = [X]; ~ ST; ST = E [ST] .nxt) 
        { 
            IF (Book [ST]) Continue ; 
            Book [ST] Book = [^ ST . 1 ] = . 1 ; // record the edge has gone through, note the two-way side 
            int Y = E [ST] .v;
             IF (D [Y] == D [X] + . 1 ) 
                ANS [ Y] = (ANS [Y] + ANS [X]% MOD) MOD%; // remember modulo! ! They accounted for 40 minutes 
            q1.push (Y); 
        } 
    } 
    for ( int I = . 1 ; I <= n-; I ++) the printf ("%d\n",ans[i]%mod);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qxds/p/11387958.html