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. linux中tar 打包指定路径文件

    linux中tar打包指定路径文件www.111cn.net 编辑:yahoo 来源:转载在linux系统中打包与解压文件我都可以使用tar命令来解决,只要使用不同的参数就可以实现不同的需要了,下面来 ...

  2. rm删除文件时排除特定文件

    删除当前目录下所有的*.txt文件,但除了test.txt文件: rm `ls *.txt | grep -v test.txt` 或者 rm `ls *.txt | egrep -v test.tx ...

  3. android网络编程之HttpUrlConnection的讲解--实现文件断点下载

    1.没有实现服务器端,下载地址为网上的一个下载链接. 2.网络开发不要忘记在配置文件中添加访问网络的权限 <uses-permission android:name="android. ...

  4. 2.请尝试安装和配置JDK,并给出安装、配置JDK的步骤。

    win10/64位 1.解压jdk1.8.0_91_x64.rar 2.同时按住win键和pause键,弹出系统属性窗口,选择高级系统设计选项. 3.然后单击环境变量按钮. 4.弹出环境变量对话框后, ...

  5. Hadoop2.6.0 动态增加节点

    本文主要从基础准备,添加DataNode和添加NodeManager三个部分详细说明在Hadoop2.6.0环境下,如何动态新增节点到集群中. 基础准备 在基础准备部分,主要是设置hadoop运行的系 ...

  6. 运维命令rsync

    如果你是一位运维工程师,你很可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能. 说到“同步”,不得不提的利器就是rsync,今天就来说说我从这个工具中看到 ...

  7. and的用法(&)

    经常看到jq的源码中各种&连接在一起赋值一个变量, 例一: var b = a&&a.getName 例二: var b = a&&a.getName||d 例 ...

  8. 重写AlertView(用block)

    @interface AlertView : UIView @property (nonatomic,copy) void(^block)(UIColor *color); - (id)initWit ...

  9. ExceL转PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...

  10. C++ concepts: Compare

    The concept Compare is a set of requirements expected by some of the standard library facilities fro ...