The 2015 China Collegiate Programming Contest

2015第一届中国大学生程序设计竞赛 F题

本质就是求单源最短路!注意会爆int

对于每一个村庄i,其实就是花费c[i],把一个人从y[i]转移到x[i];

如果一张图中,不存在w[i]==2的节点,那么花费肯定是0。

所以,花费就出在w[i]==2的节点上,怎么处理这些节点呢?

可以从w[i]==0的节点上流出一些人,流到w[i]==2的节点上,并且对于每个w[i]==2的节点只需要一个人。

因此,设立一个节点S,连向所有w[i]==0的节点,费用为0;

对于每一个村庄i,从y[i]到x[i]连边,费用为c[i];

然后从S出发,跑单源最短路,最终把S到w[i]==2的节点的最短路都加起来就是答案。

如果有一个w[i]==2的节点不能到达,那么就输出-1.

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; const long long INF=;
const int maxn=+;
int T,N,M;
int x[maxn],y[maxn];
long long c[maxn];
int w[maxn]; vector<int>G[maxn];
queue<int>Q;
bool flag[maxn];
long long dis[maxn]; struct Edge
{
int from,to;
long long cost;
} e[maxn];
int tot; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
if(!Q.empty()) Q.pop();
for(int i=; i<maxn; i++) dis[i]=INF;
memset(flag,,sizeof flag);
} void add(int x,int y,long long c)
{
tot++;
e[tot].from=x;
e[tot].to=y;
e[tot].cost=c;
G[x].push_back(tot);
} void read()
{
tot=;
init();
scanf("%d%d",&N,&M);
for(int i=; i<=N; i++) scanf("%d",&x[i]);
for(int i=; i<=N; i++) scanf("%d",&y[i]);
for(int i=; i<=N; i++) scanf("%lld",&c[i]);
for(int i=; i<=M; i++) scanf("%d",&w[i]); for(int i=; i<=N; i++) add(y[i],x[i],c[i]);
for(int i=; i<=M; i++)
{
if(w[i]==) add(,i,);
else if(w[i]==) add(i,M+,);
}
} void spfa()
{
flag[]=;
Q.push();
dis[]=;
while(!Q.empty())
{
int h=Q.front();
Q.pop();
flag[h]=;
for(int i=; i<G[h].size(); i++)
{
int id=G[h][i];
if(dis[h]+e[id].cost<dis[e[id].to])
{
dis[e[id].to]=dis[h]+e[id].cost;
if(!flag[e[id].to])
{
flag[e[id].to]=;
Q.push(e[id].to);
}
}
}
}
} int main()
{
scanf("%d",&T);
for(int Case=; Case<=T; Case++)
{
read();
spfa();
long long ans=;
for(int i=; i<=M; i++)
{
if(w[i]==)
{
if(dis[i]==INF)
{
ans=-;
break;
}
ans=ans+dis[i];
}
}
printf("Case #%d: %lld\n",Case,ans);
}
return ;
}

CDOJ UESTC 1220 The Battle of Guandu的更多相关文章

  1. CDOJ 1220 The Battle of Guandu

    The Battle of Guandu Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

  2. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  3. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  4. DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi

    题目传送门 题意:问n长度的序列,找出长度m的上升子序列的方案数. 分析:这个问题就是问:dp[i][j] = sum (dp[i-1][k]) (1 <= k <= n, a[k] &l ...

  5. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  6. UESTC 1217 The Battle of Chibi

    dp+树状数组优化. dp[i][j]表示以a[i]结尾,最长上升序列长度为j的方案数.dp[i][j]=sum{dp[k][j-1]} 其中k<i&&a[k]<a[i]. ...

  7. uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...

  8. 2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)

    赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路…… 因为无法提交,不确定是否正确..嗯..有错希望指出,谢谢... 嗯..已经A了..提交地址http://acm.uestc.ed ...

  9. CDOJ 1217 The Battle of Chibi

    The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

随机推荐

  1. css3-多列显示文字

    -------------------------- <style>            #demo{                margin: 20px auto;         ...

  2. js 数组切换图片

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  3. Moss列表查询,删除条目,更新条目。

    基于Query语句的列表查询 function retrieveListItems(itemId) {    var siteUrl=_spPageContextInfo.webServerRelat ...

  4. 在linux中创建samba,为Windows共享文件

    #首先在linux系统安装samba服务 yum install samba  共享一个目录,使用用户名和密码登录后才可以访问,要求可以读写打开samba的配置文件/etc/samba/smb.con ...

  5. IntelliJ IDEA 2016.1.3激活【亲测可用】

    测试日期:2016.6.24 License server: http://www.iteblog.com/idea/key.php // ========================= 更多技术 ...

  6. 安装apache服务出错,无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题

    错误信息:无法启动此程序,因为计算机中丢失VCRUNTIME140.dll 尝试重新安装此程序以解决此问题 错误场景:在使用["D:\Program Files\httpd-2.4.20-x ...

  7. Androidndk开发打包时我们应该如何注意平台的兼容(x86,arm,arm-v7a)

    很多朋友在开发Android JNI的的时候,会遇到findlibrary returned null的错误,因为某种原因,so没有打包到apk中.下面浅析下引起该错误的原因以及平台兼容性问题. 一. ...

  8. reactor与proactor模式

    在比较这两个模式之前,我们首先的搞明白几个概念,什么是阻塞和非阻塞,什么是同步和异步. 同步和异步是针对应用程序和内核的交互而言的. 同步是指用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪 ...

  9. 转 [分享一个SQL] 查会话阻塞关系,层次关系.

    with ash as (select /*+ materialize*/* from DBA_HIST_ACTIVE_SESS_HISTORY  where sample_time between ...

  10. UVA 10759 Dice Throwing

    题意为抛n个骰子凑成的点数和大于或等于x的概率,刚开始用暴力枚举,虽然AC了,但时间为2.227s,然后百度了下别人的做法,交了一遍,靠,0.000s,然后看了下思路,原来是dp,在暴力的基础上记忆化 ...