0 of 1 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 1 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、阅读程序,根据输入写出输出结果。 (2011年提高组)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include<iostream> using namespace std; const int V=100; int n,m,ans,e[V][V]; bool visited[V]; void dfs(int x,int len) { int i; visited[x]= true; if(len>ans) ans=len; for(i=1;i<=n;i++) if( (!visited[i]) && (e[x][i]!=-1) ) dfs(i,len+e[x][i]); visited[x]=false; } int main() { int i,j,a,b,c; cin>>n>>m; for(i=1;i<=n;i++) for(j=1;j<=m;j++) e[i][j]=-1; for(i=1;i<=m;i++) { cin>>a>>b>>c; e[a][b]=c; e[b][a]=c; } for(i=1;i<=n;i++) visited[i]=false; ans=0; for(i=1;i<=n;i++) dfs(i,0); cout<<ans<<endl; return 0; } |
输入:
4 6
1 2 10
2 3 20
3 4 30
4 1 40
1 3 50
2 4 60
输出为: