http://acm.fzu.edu.cn/problem.php?pid=2169

题目大意:

S王国有N个城市,有N-1条道路。王都为编号1的城市。叛军驻扎在很多城市。除了王都外有K个城市有军队,这K支军队要向王都进军,而且消灭沿途经过的城市中的叛军。每支军队仅仅能沿着道路走,而且是其所在城市与王都之间的最短路线走。问可以消灭多少叛军?

思路:

有两种方法。

注意到题目仅仅有N-1条边。是一颗树。

我想到的是对编号为1的结点(也就是王都,作为跟结点)进行DFS,一直遍历到树叶为止。沿途若发现有军队的,则往上传递可消灭叛军的信息。详见代码。

A了之后看了看别人的思路有方法二:

是对每一个有军队的城市进行SPFA。每次消灭一次叛军就把叛军消除(Num置为0) 直到没有叛军或者所有军队遍历完。

但我总认为怪怪的,军队向王都经过最短路径,可是方向不一定对啊!

如以下这组数据:

11 1

0 0 0 0 0 3 4 0 5 0 0



1 2 

2 3

2 4

3 5

5 6

5 7

7 8

8 9

8 10

9 11

SPFA的结果为12.而DFS的为0.

按我对题目的理解也是0.

两个代码均AC。题目含糊。。

方法一:DFS

#include<cstdio>
#include<cstring>
const int MAXN=100000+10;
const int INF=0x3ffffff;
int head[MAXN],len,n,k,ans;
int num[MAXN];
bool vis[MAXN], army[MAXN];
struct edge
{
int to,next;
}e[MAXN<<1]; void add(int from,int to)
{
e[len].to=to;
e[len].next=head[from];
head[from]=len++;
} bool dfs(int cur)
{
for(int i=head[cur];i!=-1;i=e[i].next)
{
int id=e[i].to;
if(vis[id])
continue;
vis[id]=true;
if(dfs(id))
{
if(num[id]!=0)
ans+=num[id];
return true;
}
if(army[id] ==true) //有军队
return true;
}
return false;
} int main()
{
while(~scanf("%d%d",&n,&k))
{
int cnt=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
len=0; for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i])
cnt++;
} for(int i=0;i<k;i++)
{
int temp;
scanf("%d",&temp);
army[temp]=true;
} for(int i=1;i<n;i++)
{
int from,to;
scanf("%d%d",&from,&to);
add(from,to);
add(to,from);
}
ans=0;
dfs(1);
printf("%d\n",ans);
}
return 0;
}

方法二:SPFA

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=100000+10;
const int INF=0x3ffffff;
int head[MAXN],len,n,k;
int num[MAXN], army[MAXN];
int dis[MAXN];
bool vis[MAXN];
struct edge
{
int to,next;
}e[MAXN<<1]; void add(int from,int to)
{
e[len].to=to;
e[len].next=head[from];
head[from]=len++;
} void spfa(int s)
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i]=INF;
vis[i]=false;
}
q.push(s);
dis[s]=0;
while(!q.empty())
{
int cur=q.front();
q.pop();
vis[cur]=false;
for(int i=head[cur];i!=-1;i=e[i].next)
{
int id=e[i].to;
if(dis[id] > dis[cur] + 1)
{
dis[id]=dis[cur]+1;
if(!vis[id])
{
q.push(id);
vis[id]=true;
}
}
}
}
} int main()
{
while(~scanf("%d%d",&n,&k))
{
int cnt=0;
memset(head,-1,sizeof(head));
len=0; for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i])
cnt++;
} for(int i=0;i<k;i++)
scanf("%d",&army[i]); for(int i=1;i<n;i++)
{
int from,to;
scanf("%d%d",&from,&to);
add(from,to);
add(to,from);
}
int ans=0;
for(int i=0;i<k;i++)
{
spfa(army[i]); for(int j=1;j<=n;j++)
{
if(!num[j] || dis[j]==INF) //不能到达或者没有叛军
continue;
ans+=num[j];
num[j]=0;
cnt--;
if(cnt==0)
goto next;
}
}
next:
printf("%d\n",ans);
}
return 0;
}

FZU Problem 2169 shadow的更多相关文章

  1. 福州大学 Problem 2169 shadow

    http://acm.fzu.edu.cn/problem.php?pid=2169 思路:建立一个邻接表,利用搜索中回溯把走过的路标记为1,然后把这些标记为1的值全部加起来. Problem 216 ...

  2. Problem 2169 shadow

     Problem 2169 shadow Accept: 141    Submit: 421 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Pr ...

  3. FZu Problem 2233 ~APTX4869 (并查集 + sort)

    题目链接: FZu Problem 2233 ~APTX4869 题目描述: 给一个n*n的矩阵,(i, j)表示第 i 种材料 和 第 j 种材料的影响值,这个矩阵代表这n个物品之间的影响值.当把这 ...

  4. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

  5. 翻翻棋(找规律问题)(FZU Problem 2230)

    题目是这样的: FZU Problem 2230 象棋翻翻棋(暗棋)中双方在4*8的格子中交战,有时候最后会只剩下帅和将.根据暗棋的规则,棋子只能上下左右移动,且相同的级别下,主动移动到地方棋子方将吃 ...

  6. FZU 2169 shadow (用了一次邻接表存边,树形DP)

    Accept: 28 Submit: 97 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description YL是shadow国的国 ...

  7. FZU 2169 shadow spfa

    题目链接:shadow 好佩服自己耶~~~好厉害~~~ 麻麻再也不用担心我的spfa 和 邻接表技能了~~~ spfa 记录最短路径. #include <stdio.h> #includ ...

  8. fzu Problem 2148 Moon Game(几何 凸四多边形 叉积)

    题目:http://acm.fzu.edu.cn/problem.php?pid=2148 题意:给出n个点,判断可以组成多少个凸四边形. 思路: 因为n很小,所以直接暴力,判断是否为凸四边形的方法是 ...

  9. fzu Problem 2140 Forever 0.5(推理构造)

    题目:http://acm.fzu.edu.cn/problem.php?pid=2140 题意: 题目大意:给出n,要求找出n个点,满足: 1)任意两点间的距离不超过1: 2)每个点与(0,0)点的 ...

随机推荐

  1. BZOJ 4199: [Noi2015]品酒大会( 后缀数组 + 并查集 )

    求出后缀数组后, 对height排序, 从大到小来处理(r相似必定是0~r-1相似), 并查集维护. 复杂度O(NlogN + Nalpha(N)) ------------------------- ...

  2. USACO Section 4.2 Drainage Ditches(最大流)

    最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...

  3. 浅谈 non-blocking I/O Multiplexing + poll/epoll 的正确使用

    在前面的文章中曾经粗略讲过poll,那时是用阻塞IO实现,在发送和接收数据量都较小情况下和网络状况良好的情况下是基本没有问题的,read 不会只接收部分数据,write 也不会一直阻塞.但实际上pol ...

  4. Spring集成Quartz定时器

    <!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...

  5. iOS 相关职位要求整理版

    在拉勾上找了20家,BOSS直聘找了10家感兴趣的在招聘 iOS 程序员的公司,把职位要求整理了一下. 初创公司一般要求1年以上开发经验,成长型或者成熟型公司一般要求最低2年以上开发经验.这里针对的是 ...

  6. 看日记学git摘要~灰常用心的教程

    看日记学git linux 命令行 cd ls / ls -a clear mkdir rmdir echo "hi, good day" > hi.txt touch he ...

  7. HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)

    一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客 ...

  8. Qt监测光驱变化(使用WM_DEVICECHANGE)

    xxx.h protected: bool winEvent(MSG *msg,long * result); xxx.cpp bool CBlurayTranscoderDlg::winEvent( ...

  9. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

  10. c++ 覆盖、重载、隐藏

    函数重载: 1.相同的范围内(即同一类中) 2.函数名相同: 3.参数不同: 4.virtual关键字可有可无: 函数覆盖:虚函数的功能.动态多态 (父类中必须有virtual)========派生类 ...