P1294 go for a walk, P1294 go for a walk

Source: Internet
Author: User

P1294 go for a walk, P1294 go for a walk
Background

Master recently fell in love. It's just lovesickness. "Even a Lovesickness is a pure love. Today, this sunny morning, the sun slowly rises from the West. As a result, he found the master and hoped to walk with the master on the top of the hill before the morning reading. The master will certainly not give up the opportunity he dreamed of. He has prepared everything.

Description

There are n viewing points on the top of the hill, and there are m walking trails between the viewing points. The master doesn't like the exciting process, so those who have no way to view the points won't choose to go. In addition, she does not like to go to the same viewing point more than once. The experts want to make them the longest journey together (when they look at the view, it will not take care of the master). The known experts can let them start at any viewing point, it also ends at any viewing point.

Input/Output Format Input Format:

The first line is the integer n, m, and m separated by spaces, which indicates the information of each trail: the number and length of the viewing points at both ends.

Output Format:

An integer indicates the longest journey they can accompany.

Input and Output sample Input example #1:
4 61 2 102 3 203 4 304 1 401 3 502 4 60
Output sample #1:
150
Description

For 100% of the data: n ≤ 20, m ≤ 50, ensure that there are no multiple trails between the viewing points.

 

Dfs brute force!

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 void read(int & n) 7 { 8     char c='+';int x=0;bool flag=0; 9     while(c<'0'||c>'9')10     {11         c=getchar();12         if(c=='-')flag=1;    13     }14     while(c>='0'&&c<='9')15         x=x*10+c-48,c=getchar();16     flag==1?n=-x:n=x;17 }18 const int MAXN=100001;19 struct node20 {21     int u,v,w,nxt;22 }edge[MAXN];23 int head[MAXN];24 int num=1;25 int n,m;26 int vis[MAXN];27 int ans=0;28 void add_edge(int x,int y,int z)29 {30     edge[num].u=x;31     edge[num].v=y;32     edge[num].w=z;33     edge[num].nxt=head[x];34     head[x]=num++;35 }36 void dfs(int p,int now)37 {38     ans=max(ans,now);39     vis[p]=1;40     for(int i=head[p];i!=-1;i=edge[i].nxt)41         if(vis[edge[i].v]==0)42             dfs(edge[i].v,now+edge[i].w);43     vis[p]=0;44 }45 int main()46 {47     read(n);read(m);48     for(int i=1;i<=n;i++)head[i]=-1;49     for(int i=1;i<=m;i++)50     {51         int x,y,z;52         read(x);read(y);read(z);53         add_edge(x,y,z);54         add_edge(y,x,z);55     }56     for(int i=1;i<=n;i++)57     {58         memset(vis,0,sizeof(vis));59         dfs(i,0);60     }61     printf("%d",ans);62     return 0;63 }

 

Related Article

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.