您的位置:首页 > 编程语言

【bzoj1602】【Usaco2008 Oct】牧场行走 (暴力) 题解&代码

2016-05-16 09:37 525 查看
题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1602

题解:

这题。。。。我也不想多说什么,这数据范围。。直接暴力啊!!!

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int n,q,tot,dp[1005],dis[1005],flag;
struct node{
int v;int w;int next;
}edge[2005];
int head[1005],st,ed;
void addedge(int u,int v,int d)
{
tot++;
edge[tot].v=v;
edge[tot].w=d;
edge[tot].next=head[u];
head[u]=tot;
tot++;
edge[tot].v=u;
edge[tot].w=d;
edge[tot].next=head[v];
head[v]=tot;
}
void dfs(int x,int fa,int d)
{
dis[x]=d;
if (x==ed) flag=1,printf("%d\n",d);
if (flag) return ;
for (int i=head[x];i!=0;i=edge[i].next)
{
int v=edge[i].v;
if (v!=fa)
dfs(v,x,d+edge[i].w);
}
}
int main()
{
scanf("%d%d",&n,&q);
tot=1;
for (int i=1;i<n;i++)
{
int x,y,d;
scanf("%d%d%d",&x,&y,&d);
addedge(x,y,d);
}
for (int i=1;i<=q;i++)
{
scanf("%d%d",&st,&ed);
flag=0;
dfs(st,st,0);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: