Codevs 1664 cool water, codevs1664 cold water

Source: Internet
Author: User

Codevs 1664 cool water, codevs1664 cold water
1664 Refreshing cold water

 

Time Limit: 1 s space limit: 128000 KB title level: Gold Title Description Description

In the hot summer, dairy areas in Wisconsin provide cold water for cows to quench their thirst. Farmer John sent the cold water to the cowshed through N (3 <= N <= 99999; N is an odd number) cold water pipes numbered 1 .. N from the pump position. When water flows in a pipe, summer heat heats it. Bessie wants to find the coldest water so that she can enjoy the rare good weather better than any other cow.

She has drawn a complete set of branch pipelines, and noticed that the pipeline system is like a tree where its roots are on the farm and each branch is separated from each other from the root. Surprisingly, all pipelines have a length. Of course, all N pipelines are connected to one or other pipeline routes.

Map the connections of all pipelines and calculate the distance from each pivot point to the cowshed. Bessie will use this information to find the coolest cold water.

The endpoint of the MPs queue, which can be used as the pivot point or the endpoint of the MPs queue. The endpoint is named after the MPs queue number. The map contains C (1 <= C <= N) splitters, each of which contains 3 data, and the MPs queue endpoint E_ I (1 <= E_ I <= N ), b1_ I, B2_ I (2 <= B1_ I <= N; 2 <= B2_ I <= N) of the two MPs queues connected by the MPs queue endpoint ). Pipe 1 is connected to the cowshed, and the pipe length between each two connectors is 1.

Input description Input Description

* Row 1st: two integers N and C separated by Spaces

* Lines 2nd to C + 1: Three integers separated by spaces, respectively indicating the number of the connector and the number E_ I, binii, B2_ I of the two connected pipelines

Output description Output Description

* N rows in total: the shortest distance from each pipe to the cowshed.

Sample Input Sample Input

5 2

3 5 4

1 2 3

Sample output Sample Output

1

2

2

3

3

Data range and prompt Data Size & Hint

+ ------ +

| Barn |

+ ------ +

| 1

*

2/\ 3

*

4/\ 5

CATEGORY tag Tags click here to expand

Basically, it is a bare SPFA, but you need to add data processing.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 const int MAXN=100001; 7 const int maxn=0x7fffffff; 8 struct node 9 {10     int u;11     int v;12     int w;13     int next;14 }edge[MAXN];15 int num=1;16 int head[MAXN];17 int dis[MAXN];18 int vis[MAXN];19 void spfa()20 {21     dis[1]=0;22     vis[1]=0;23     queue<int>q;24     q.push(1);25     while(q.size()!=0)26     {27         int p=q.front();28         q.pop();29         for(int i=head[p];i!=-1;i=edge[i].next)30         {31             int to=edge[i].v;32             if(dis[to]>dis[p]+edge[i].w)33             {34                 dis[to]=dis[p]+edge[i].w;35                 if(vis[to]==0)36                 {37                     vis[to]=1;38                     q.push(to);39                 }40             }41         }42     }43 }44 int main()45 {46     int n,m;47     scanf("%d%d",&n,&m);48     for(int i=1;i<=n;i++)49     {50         head[i]=-1;51         dis[i]=maxn;52     }53     for(int i=1;i<=m;i++)54     {55         int x,y,z;56         scanf("%d%d%d",&x,&y,&z);57         edge[num].u=x;58         edge[num].v=y;59         edge[num].w=1;60         edge[num].next=head[x];61         head[x]=num++;62         edge[num].u=x;63         edge[num].v=z;64         edge[num].w=1;65         edge[num].next=head[x];66         head[x]=num++;67     }68     spfa();69     for(int i=1;i<=n;i++)70     {71         printf("%d\n",dis[i]+1);72     }73     return 0;74 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.