2014 Super Training #10 C Shadow --SPFA/随便搞/DFS
原题: 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的更多相关文章
- 2014 Super Training #10 G Nostop --矩阵快速幂
		
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
 - 2014 Super Training #10 D 花生的序列 --DP
		
原题: FZU 2170 http://acm.fzu.edu.cn/problem.php?pid=2170 这题确实是当时没读懂题目,连样例都没想通,所以没做了,所以还是感觉这样散漫的做不好,有些 ...
 - 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
		
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
 - 2014 Super Training #7 C Diablo III --背包问题(DP)
		
原题: ZOJ 3769 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 一个带有一些限制的背包问题. 假设在没有限 ...
 - 2014 Super Training #6 B Launching the Spacecraft --差分约束
		
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
 - 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构成的格子,对 ...
 - 2014 Super Training #3 H Tmutarakan Exams --容斥原理
		
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
 - 2014 Super Training #8 B Consecutive Blocks --排序+贪心
		
当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...
 - 2014 Super Training #8 A Gears --并查集
		
题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...
 
随机推荐
- 【转】 StringUtils中 isNotEmpty 和isNotBlank的区别
			
[转自]http://blog.csdn.net/foamflower/article/details/5713604 isNotEmpty将空格也作为参数,isNotBlank则排除空格参数 Str ...
 - 【iOS】Quartz2D图片剪切
			
一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - (void)drawRect:(CGRect)rec ...
 - 各个平台的mysql重启命令
			
linux平台及windows平台mysql重启方法 Linux下重启MySQL的正确方法: 1.通过rpm包安装的MySQL service mysqld restart 2.从源码包安装的MySQ ...
 - dbcp/c3p0连接池设置mysql会话变量
			
我们有几个计算风控值的定时任务,几乎每隔5秒会更新所有账户的当前总资产并以此通知风控,每隔一小时就产生一两个G的binlog,几十台服务器折腾..数据库是公用的,代码是通过工具自动生成的,直接修改流程 ...
 - Linux初学者指南
			
1.为啥我们要学习Linux? 我们干嘛要学习Linux? Linux能给我们带来什么价值呢? Linux给我的感觉就是稳定,免费,性能好. 稳定,体现在哪里?我们使用PC机,安装的操作系统一般是wi ...
 - Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法
			
Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法 Some sites could ...
 - ASP.NET MVC的请求生命周期
			
我希望能理解在浏览器输入URL并敲击回车来请求一个ASP.NET MVC网站的页面之后发生的任何事情. 为什么需要关心这些?有两个原因.首先是因为ASP.NET MVC是一个扩展性非常强的框架.例如, ...
 - mvc项目架构分享系列之架构搭建初步
			
mvc项目架构分享系列之架构搭建初步 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 ...
 - jQuery知识大杂汇
			
1.jQuery 语法是通过选取 HTML 元素,并对选取的元素执行某些操作. 基础语法: $(selector).action() 举几枚栗子吧: $(this).hide() - 隐藏当前元素 $ ...
 - mysql innoDB 挂了的临时解决方案
			
Mysql InnoDB: Error: checksum mismatch by Mattias Hemmingsson on December 23, 2013 in Linux • 6 Comm ...