https://www.luogu.org/problem/P4779 a template on the topic off the valley
Topic background
July 19, 2018, certain students in NOI Day 1 T1 way out a problem in a very proficient in the use of a well-known algorithm for finding the shortest path.
and then?
100 \rightarrow 60100→60;
Ag \rightarrow CuAg→Cu;
Eventually, he therefore could not reach a contract with the ideal university.
Small F no longer wish to repeat.
Title Description
Given a N N points, M M article nonnegative FIG right side to the tape, you calculate from S departure S, the distance to each point.
Data guarantee you from S S to an arbitrary starting point.
Input Format
The first three positive integers behavior N, M, S N , M , S. The second row from M M rows, each row of three non-negative integer u_i, V_I, W_i U I , V I , W I , represents from u_i U I to V_I V I has a weight value W_i W i edges.
Output Format
Output line N N spaces separated by non-negative integer S S distance to each point.
Sample input and output
4 6 1 1 2 2 2 3 2 2 4 1 1 3 5 3 4 3 1 4 4
0243
The subject wa a lot of hair. . . The reason is that no in-depth understanding of the meaning of the queue! ! Oh ~ ~
#include<iostream> #include<cstdio> #include<vector> #include<queue> #include<cstring> using namespace std; const int N=1E5+7; const int INF=0x3f3f3f3f; typedef long long ll; struct stu{ ll a,b; }; ll dis[N]; int mark[N]; vector<stu>ve[N]; struct node{ int x,y; bool friend operator<(const node x1,const node y1){ return x1.y>y1.y; } }; void djstrea(int s){ memset(mark,0,sizeof(mark)); memset(dis,INF,sizeof(dis)); priority_queue<node>que; dis[s]=0; que.push({s,0}); while(que.size()){ node xx=que.top(); que.pop(); if(mark[xx.x]==1) continue ; mark[xx.x]=1; for(int i=0;i<ve[xx.x].size();i++){ int dx=ve[xx.x][i].a; int dy=ve[xx.x][i].b; if(mark[dx]==0&&dis[dx]>dis[xx.x]+dy){ dis[dx]=dis[xx.x]+dy; if(mark[dx]==0) que.push({dx,dis[dx]}); } } } } int main(){ int n,m,s; cin>>n>>m>>s; int x,y,z; for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); ve[x].push_back({y,z}); // ve[y].push_back({x,z});//单向能过。双向应该也可以的 } djstrea(s); for(int i=1;i<=n;i++) cout<<dis[i]<<" "; return 0; }