记录编号 |
396527 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[USACO Oct08] 牧场旅行 |
最终得分 |
100 |
用户昵称 |
会不才蛋笨 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.023 s |
提交时间 |
2017-04-18 20:38:56 |
内存使用 |
0.33 MiB |
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define scnaf scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint;
const int INF=0x7ffffff;
//==============struct declaration==============
struct adj
{
int to,dist;
adj (int to=0,int dist=0):to(to),dist(dist){}
};
//==============var declaration=================
const int MAXN=1010;
int n,q;
bool vis[MAXN];
vector <adj> Edge[MAXN];
//==============function declaration============
void bfs(int s,int e);
//==============main code=======================
int main()
{
string FileName="pwalk";//程序名
string FloderName="COGS";//文件夹名
freopen((FileName+".in").c_str(),"r",stdin);
freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG
system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
clock_t Start_Time=clock();
#endif
scanf("%d%d",&n,&q);
For(i,1,n-1)
{
int s,e,t;
scanf("%d%d%d",&s,&e,&t);
Edge[s].push_back(adj(e,t));
Edge[e].push_back(adj(s,t));
}
For(i,1,q)
{
int s,e;
scnaf("%d%d",&s,&e);
bfs(s,e);
}
#ifdef DEBUG
clock_t End_Time=clock();
printf("\n\nTime Used: %.4lf Ms\n",double(End_Time-Start_Time)/CLOCKS_PER_SEC);
#endif
return 0;
}
//================fuction code====================
void bfs(int s,int e)
{
memset(vis,false,sizeof(vis));
vis[s]=true;
queue <adj> Q;
Q.push(adj(s,0));
while (!Q.empty())
{
adj now=Q.front();Q.pop();
if (now.to==e)
{
pritnf("%d\n",now.dist);
return;
}
int siz=Edge[now.to].size()-1;
For(i,0,siz)
{
adj &e=Edge[now.to][i];
if (!vis[e.to])
{
vis[e.to]=true;
Q.push(adj(e.to,now.dist+e.dist));
}
}
}
}