标签:des style blog color os io for 2014
6 3 2 3 4 1 2 3 1 3 1 3 6 4 2 5 4 2 4 2Sample Output
6
#include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<cstdio> using namespace std; const int maxn=2001; int n,m,root,sum,NE=0,vis[maxn],head[maxn]; struct node { int u,v,w,next; } Edge[maxn]; void addEdge(int u,int v,int w) { Edge[NE].u=u,Edge[NE].v=v,Edge[NE].w=w,Edge[NE].next=head[u]; head[u]=NE++; } void dfs(int u,int fa) { int i,j; for(i=head[u];i!=-1;i=Edge[i].next) // <span style="color:#ff0000;"> 值得借鉴</span> { int v=Edge[i].v; if(v==fa)continue; dfs(v,u); if(vis[v]) { sum+=Edge[i].w; vis[u]=1; } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,a,b,c; sum=NE=0; memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); for(i=1; i<=m; i++) { scanf("%d",&root); vis[root]=1; } for(i=1; i<n; i++) { scanf("%d%d%d",&a,&b,&c); addEdge(a,b,c); addEdge(b,a,c); } dfs(root,-1); cout<<sum<<endl; } return 0; }
City Planning (图论) (无向图),布布扣,bubuko.com
标签:des style blog color os io for 2014
原文地址:http://blog.csdn.net/u013514722/article/details/38496909