码迷,mamicode.com
首页 > 其他好文 > 详细

Lightoj 1174 - Commandos (bfs)

时间:2015-08-17 21:15:37      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

  Lightoj  1174 - Commandos

题目描述:
  有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营。他们计划要在敌人的每一个军营里都放置一个炸弹。军营里有充足的士兵,每个士兵都可以携带充足的炸弹,问这个军队完成任务最少需要时间?(假设士兵在往敌军帐篷里放炸弹时,敌军不会防御。敌人是猪嘛?这是在和猪战斗嘛?

解题思路:
  水题,求士兵完成任务的最短时间,也就是说每个士兵执行任务的时候只能选择最优路径咯。但是又要满足每个点都要被走过,所以就要求出能覆盖所有点的最短路径里最长一条的长度咯。只需要从起点bfs一遍所有的点,然后从终点bfs一遍所有的点。辣么从起点到终点并且经过x的最短路径长度就是两次bfs出来的结果和咯。

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 105;
 8 struct node
 9 {
10     int to, next;
11 }edge[maxn*maxn];
12 int head[maxn], tot, a[maxn], b[maxn];
13 
14 void Add (int from, int to)
15 {
16     edge[tot].to = to;
17     edge[tot].next = head[from];
18     head[from] = tot ++;
19 }
20 
21 void bfs (int s, int vis[])
22 {
23     queue <int> Q;
24     Q.push(s);
25     vis[s] = 0;
26     while (!Q.empty())
27     {
28         int u = Q.front();
29         Q.pop();
30         for (int i=head[u]; i!=-1; i=edge[i].next)
31         {
32             int v = edge[i].to;
33             if (vis[v]==-1)
34             {
35                 vis[v] = vis[u] + 1;
36                 Q.push (v);
37             }
38         }
39     }
40 }
41 
42 int main ()
43 {
44     int t, n, r, s, e, cas = 1;
45     scanf ("%d", &t);
46 
47     while (t --)
48     {
49         scanf ("%d %d", &n, &r);
50         memset (head, -1, sizeof(head));
51         tot = 0;
52 
53         for (int i=0; i<r; i++)
54         {
55             int u, v;
56             scanf ("%d %d", &u, &v);
57             Add (u, v);
58             Add (v, u);
59         }
60         
61         scanf ("%d %d", &s, &e);
62         memset (a, -1, sizeof(a));
63         memset (b, -1, sizeof(b));
64         bfs (s, a);
65         bfs (e, b);
66         
67         int ans = 0;
68         for (int i=0; i<n; i++)
69             ans = max (ans, a[i]+b[i]);
70         printf("Case %d: %d\n", cas++, ans);
71     }
72     return 0;
73 }

Lightoj 1174 - Commandos (bfs)

标签:

原文地址:http://www.cnblogs.com/alihenaixiao/p/4737512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!