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

这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边,用向量表示的话会TLE,而且用SPFA有一个异或,就是题目说要沿最短路走到都城,但是SPFA是走最短路去消灭叛军,然后再走回都城,我不知道怎么回事,不知道能不能有大神解释。因为这样的话,有多少叛军就能消灭多少叛军了,那就不要用什么算法 了,直接一个统计。于是试了一下,居然A了,瞬间变成大水题,我无法再评价这个题目了,就当消遣了。

SPFA法:依次从每个军队城市出发做一次SPFA,看到有能到的(肯定能到啊)叛军就将其消灭。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#define Mod 1000000007
using namespace std;
#define N 100007 struct Edge
{
int v,next;
}G[*N];
int head[N],tot;
int army[N],rebel[N];
int vis[N],dis[N];
int res;
int n,m; void addedge(int u,int v)
{
G[tot].v = v;
G[tot].next = head[u];
head[u] = tot++;
} void SPFA(int s)
{
int i;
memset(vis,,sizeof(vis));
queue<int> que;
while(!que.empty())
que.pop();
que.push(s);
vis[s] = ;
dis[s] = ;
while(!que.empty())
{
int tmp = que.front();
que.pop();
vis[tmp] = ;
for(i=head[tmp];i!=-;i=G[i].next)
{
int v = G[i].v;
if(dis[v] > dis[tmp] + )
{
dis[v] = dis[tmp]+;
if(!vis[v])
{
que.push(v);
vis[v] = ;
}
}
}
}
} int main()
{
int i,j,x;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(army,,sizeof(army));
memset(rebel,,sizeof(rebel));
memset(head,-,sizeof(head));
tot = ;
int cnt = ;
for(i=;i<=n;i++)
{
scanf("%d",&rebel[i]);
if(rebel[i])
cnt++;
}
for(i=;i<=m;i++)
scanf("%d",&army[i]);
for(i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
res = ;
for(i=;i<=m;i++)
{
SPFA(army[i]);
for(j=;j<=n;j++)
{
if(dis[j] != Mod)
{
if(rebel[j])
{
res += rebel[j];
rebel[j] = ;
cnt--;
}
}
}
if(cnt == ) //已经消灭完
break;
}
printf("%d\n",res);
}
return ;
}

DFS法(选GNU C++ 会Runtime Error,要选Visual C++):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define Mod 1000000007
#define ll long long
using namespace std;
#define N 100007 struct Edge
{
int v,next;
}G[*N];
int head[N],tot;
int army[N],rebel[N];
ll sum;
int n,m; void addedge(int u,int v)
{
G[tot].v = v;
G[tot].next = head[u];
head[u] = tot++;
} int dfs(int u,int val,int fa)
{
int soni = ;
if(army[u]) //找到军队
{
sum += val;
soni++;
}
for(int i=head[u];i!=-;i=G[i].next)
{
int v = G[i].v;
if(v == fa)
continue;
soni += dfs(v,val+rebel[u],u);
}
if(soni > )
sum -= (soni-)*rebel[u];
return soni;
} int main()
{
int i,j,x;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(army,,sizeof(army));
memset(rebel,,sizeof(rebel));
memset(head,-,sizeof(head));
tot = ;
for(i=;i<=n;i++)
scanf("%d",&rebel[i]);
for(i=;i<=m;i++)
{
scanf("%d",&x);
army[x] = ;
}
for(i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
sum = ;
dfs(,,-);
printf("%lld\n",sum);
}
return ;
}

直接统计:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define N 100007 int army[N],rebel[N];
int res;
int n,m; int main()
{
int i,j,x;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(rebel,,sizeof(rebel));
int cnt = ;
res = ;
for(i=;i<=n;i++)
{
scanf("%d",&rebel[i]);
if(rebel[i])
res += rebel[i];
}
for(i=;i<=m;i++)
scanf("%d",&army[i]);
for(i=;i<n-;i++)
scanf("%d%d",&u,&v);
if(m == )
{
puts("");
continue;
}
printf("%d\n",res);
}
return ;
}

2014 Super Training #10 C Shadow --SPFA/随便搞/DFS的更多相关文章

  1. 2014 Super Training #10 G Nostop --矩阵快速幂

    原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...

  2. 2014 Super Training #10 D 花生的序列 --DP

    原题: FZU 2170 http://acm.fzu.edu.cn/problem.php?pid=2170 这题确实是当时没读懂题目,连样例都没想通,所以没做了,所以还是感觉这样散漫的做不好,有些 ...

  3. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  4. 2014 Super Training #7 C Diablo III --背包问题(DP)

    原题: ZOJ 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 一个带有一些限制的背包问题. 假设在没有限 ...

  5. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  6. 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS

    原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...

  7. 2014 Super Training #3 H Tmutarakan Exams --容斥原理

    原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...

  8. 2014 Super Training #8 B Consecutive Blocks --排序+贪心

    当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...

  9. 2014 Super Training #8 A Gears --并查集

    题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...

随机推荐

  1. AngularJs Cookie 的使用

    最新在学习 AngularJs ,发现网上很难搜到 AngularJs.Cookie 教程, 就自己写篇博客,希望能帮到刚学的人. 废话不多说上代码 首先要引用 angular-cookies.js ...

  2. .NET Core应用程序的2种部署方式

    1. Portable 共享.NET Core运行时环境与程序集依赖,部署的目标机器上需要事先安装.NET Core SDK,这是.NET Core的默认部署方式. 2. Self-contained ...

  3. Android5.0新特性——兼容性(support)

    兼容性 虽然Material Design新增了许多新特性,但是并不是所有新内容对对下保持了兼容. 使用v7包 v7 support libraries r21 及更高版本包含了以下Material ...

  4. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

  5. js中的排序

    不靠谱的sort() 众所周知,js中的sort()排序是按字母表顺序排序的,这就导致如下现象: var a = [9,60,111,55,8,7777]; a.sort(); alert(a); / ...

  6. webservice 的wsdl文件生成客户端java类

    提供两个方法: 第一个: 发布webservice项目后, 地址栏地址  http://localhost:8888/lxitedu.webservice.cxf-ch2/services/userS ...

  7. HTML <map> 标签-创建带有可点击区域的图像映射

    定义和用法 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 所有主流浏览器都支持 <map> 标签. 注释:area 元素永远嵌套在 map 元素内部. ...

  8. ASP.Net页面刷新后自动滚动到原来位置

    在网上搜索之后总结了三种方式: 1.设置Page中的MaintainScrollPositionOnPostback属性为true A>.页面里有MaintainScrollPositionOn ...

  9. Android Studio用release模式进行调试

    有时候调试SDK必须要用release版本,但是每次打包混淆太麻烦,希望能在IDE中直接跑出release版本的应用,简单来说就是在debug模式下产生release版本的app,这时候该怎么做呢?当 ...

  10. App开发流程之字符串处理工具类

    记录字符串的处理,不是一个简单的工作. NSString是代码中随处可见的类型,也是应用和处理繁多的对象,在此只记录需要常备的方法,并且加以说明. #pragma mark -- [计算字符串尺寸 + ...