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的相对距离的奇偶性). ...
随机推荐
- PHP json编码遇到的问题
今天遇到了json编码解码之后中文消失的问题,一探究竟,写下这篇文章 PHP中提供了json_encode 和json_decode 这对函数 将PHP中 值转化成 字符串,但是遇到中文的时候,很容 ...
- python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...
- mybatis3批量更新 批量插入
在公司ERP项目开发中,遇到批量数据插入或者更新,因为每次连接数据库比较耗时,所以决定改为批量操作,提升效率.库存盘点导入时,需要大量数据批量操作. 1:数据库连接代码中必须开启批量操作.加上这句,& ...
- domain规划
user-generated content 用户产生内容 和 admin-generated content 管理员产生内容,要区分开来,便于不同的图片压缩.备份.带宽.CDN.审核方案. 图片(j ...
- JS中检测数据类型的几种方式及优缺点
1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...
- css权重是什么
css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...
- echarts 地图图
效果如下图: 1.下载echarts对应包: http://echarts.baidu.com/ 2.前端页面: <!DOCTYPE html> <html xmlns=" ...
- R语言学习笔记:因子
R语言中的因子就是factor,用来表示分类变量(categorical variables),这类变量不能用来计算而只能用来分类或者计数. 可以排序的因子称为有序因子(ordered factor) ...
- iOS NSNumber转化NSString之description
我们经常需要把一个数字转成字符串,当你不需要配合其他字符串的时候可以用description. /** description属于NSObject 值是NSNumber时候,不用stringWithF ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...