Skip to main content
  1. Posts/

bzoj 1602: [Usaco2008 Oct]牧场行走 (bfs,优先队列)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

Description

N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。

Input

*第一行:两个被空格隔开的整数:N和Q

*第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI

*第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。

Output

*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。

Sample Input

4 2

2 1 2

4 3 2

1 4 3

1 2

3 2

Sample Output

2

7

思路:直接bfs….貌似因为每个点最多只和两个边相连。。。不用优先队列也行? 1A,好爽23333.

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年03月31日 星期四 20时27分01秒
  4File Name :code/bzoj/1602.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=1E3+5;
 34int n,q;
 35vector< pi >edge[N];
 36bool vis[N];
 37int d[N];
 38
 39struct node
 40{
 41    int x;
 42    int d;
 43
 44    bool operator < (node b)const
 45    {
 46	return d>b.d;
 47    }
 48};
 49
 50int bfs( int s,int t)
 51{
 52    priority_queue<node>q;
 53    ms(vis,false);
 54    node tmp;
 55    tmp.x = s;
 56    tmp.d = 0 ;
 57    q.push(tmp);
 58    vis[s] = true;
 59    while (!q.empty())
 60    {
 61	node pre = q.top();q.pop();
 62	if (pre.x==t) return pre.d;
 63	for ( int i = 0 ; i < int(edge[pre.x].size()) ; i++)
 64	{
 65	    node nxt;
 66	    nxt.x= edge[pre.x][i].fst;
 67	    nxt.d = pre.d + edge[pre.x][i].sec;
 68	    if (!vis[nxt.x])
 69	    {
 70
 71		q.push(nxt);
 72		vis[nxt.x] = true;
 73	    }
 74	}
 75    }
 76    return -1;
 77
 78}
 79int main()
 80{
 81	#ifndef  ONLINE_JUDGE
 82	freopen("code/in.txt","r",stdin);
 83  #endif
 84
 85	ios::sync_with_stdio(false);
 86	cin>>n>>q;
 87	for ( int i = 1 ; i <= n-1 ; i++)
 88	{
 89	    int u,v,w;
 90	    cin>>u>>v>>w;
 91	    edge[u].push_back(make_pair(v,w));
 92	    edge[v].push_back(make_pair(u,w));
 93	}
 94
 95	while (q--)
 96	{
 97	    int s,t;
 98	    cin>>s>>t;
 99	    int ans = bfs(s,t);
100	    cout<<ans<<endl;
101	}
102
103
104  #ifndef ONLINE_JUDGE
105  fclose(stdin);
106  #endif
107    return 0;
108}

Related

I - Fire Game (两个点开始的bfs)

·3 mins
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83084#problem/I I - Fire Game **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

poj 3414 pots (bfs+路径记录)

·1 min
好爽,一遍ac 1 2 3 /************************************************************************* 4 > File Name: code/2015summer/searching/H.cpp 5 > Author: 111qqz 6 > Email: rkz2013@126.com 7 > Created Time: 2015年07月27日 星期一 09时11分28秒 8 ************************************************************************/ 9 10 #include<iostream> 11 #include<iomanip> 12 #include<cstdio> 13 #include<algorithm> 14 #include<cmath> 15 #include<cstring> 16 #include<string> 17 #include<map> 18 #include<set> 19 #include<queue> 20 #include<vector> 21 #include<stack> 22 #define y0 abc111qqz 23 #define y1 hust111qqz 24 #define yn hez111qqz 25 #define j1 cute111qqz 26 #define tm crazy111qqz 27 #define lr dying111qqz 28 using namespace std; 29 #define REP(i, n) for (int i=0;i<int(n);++i) 30 typedef long long LL; 31 typedef unsigned long long ULL; 32 const int N=1E2+5; 33 int A,B,C; 34 int d[N][N]; 35 bool flag; 36 struct node 37 { 38 int d,opt,par,prea,preb; 39 }q[N][N]; 40 41 void print(int x,int y) 42 { 43 // cout<<"x:"<<x<<"y:"<<y<<endl; 44 if (q[x][y].prea!=-1&&q[x][y].preb!=-1) 45 { 46 // cout<<"who is 111qqz"<<endl; 47 print(q[x][y].prea,q[x][y].preb); 48 if (q[x][y].opt==1){ 49 printf("FILL(%d)\n",q[x][y].par); 50 } 51 if (q[x][y].opt==2) 52 { 53 printf("DROP(%d)\n",q[x][y].par); 54 } 55 if (q[x][y].opt==3) 56 { 57 printf("POUR(%d,%d)\n",q[x][y].par,3-q[x][y].par); 58 } 59 } 60 61 } 62 void bfs() 63 { 64 memset(q,-1,sizeof(q)); 65 queue<int>a; 66 queue<int>b; 67 a.push(0); 68 b.push(0); 69 q[0][0].d=0; 70 while (!a.empty()&&!b.empty()) 71 { 72 int av = a.front();a.pop(); 73 int bv = b.front();b.pop(); 74 // cout<<"av:"<<av<<"bv:"<<bv<<endl; 75 if (av==C||bv==C) 76 { 77 flag = true; 78 //cout<<"yeah~~~~~~~~~~~~~~~"<<endl; 79 cout<<q[av][bv].d<<endl; 80 // cout<<"prea:"<<q[av][bv].prea<<"preb:"<<q[av][bv].preb<<endl; 81 print(av,bv); 82 return; 83 } 84 if (av<A&&q[A][bv].d==-1) 85 { 86 q[A][bv].d=q[av][bv].d+1; 87 q[A][bv].opt=1; 88 q[A][bv].par=1; 89 q[A][bv].prea=av; 90 q[A][bv].preb=bv; 91 a.push(A); 92 b.push(bv); 93 } 94 if (av>0&&q[0][bv].d==-1) 95 { 96 q[0][bv].d=q[av][bv].d+1; 97 q[0][bv].opt=2; 98 q[0][bv].par=1; 99 q[0][bv].prea=av; 100 q[0][bv].preb=bv; 101 a.push(0); 102 b.push(bv); 103 104 } 105 if (bv<B&&q[av][B].d==-1) 106 { 107 q[av][B].d=q[av][bv].d+1; 108 q[av][B].opt=1; 109 q[av][B].par=2; 110 q[av][B].prea = av; 111 q[av][B].preb = bv; 112 a.push(av); 113 b.push(B); 114 115 } 116 if (bv>0&&q[av][0].d==-1) 117 { 118 q[av][0].d=q[av][bv].d+1; 119 q[av][0].opt=2; 120 q[av][0].par=2; 121 q[av][0].prea=av; 122 q[av][0].preb=bv; 123 a.push(av); 124 b.push(0); 125 } 126 127 if (av+bv<=B&&q[0][av+bv].d==-1) 128 { 129 q[0][av+bv].d=q[av][bv].d+1; 130 q[0][av+bv].opt=3; 131 q[0][av+bv].par=1; 132 q[0][av+bv].prea=av; 133 q[0][av+bv].preb=bv; 134 a.push(0); 135 b.push(av+bv); 136 } 137 if (av+bv>B&&q[av-(B-bv)][B].d==-1) //把1往2里倒入的两种情况 138 { 139 140 int tmp = av-(B-bv); 141 q[tmp][B].d=q[av][bv].d+1; 142 q[tmp][B].opt=3; 143 q[tmp][B].par=1; 144 q[tmp][B].prea=av; 145 q[tmp][B].preb=bv; 146 a.push(tmp); 147 b.push(B); 148 } 149 150 if (bv+av<=A&&q[av+bv][0].d==-1) 151 { 152 q[av+bv][0].d=q[av][bv].d+1; 153 q[av+bv][0].opt=3; 154 q[av+bv][0].par=2; 155 q[av+bv][0].prea=av; 156 q[av+bv][0].preb=bv; 157 a.push(av+bv); 158 b.push(0); 159 } 160 if (bv+av>A&&q[A][bv-(A-av)].d==-1) 161 { 162 int tmp = bv-(A-av); 163 q[A][tmp].d=q[av][bv].d+1; 164 q[A][tmp].opt=3; 165 q[A][tmp].par=2; 166 q[A][tmp].prea=av; 167 q[A][tmp].preb=bv; 168 a.push(A); 169 b.push(tmp); 170 } 171 } 172 } 173 int main() 174 { 175 176 flag = false; 177 cin>>A>>B>>C; 178 bfs(); 179 if (!flag) 180 { 181 cout<<"impossible"<<endl; 182 } 183 184 185 return 0; 186 }