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. Chapter 2 Open Book——6

    Last night I'd discovered that Charlie couldn't cook much besides friedeggs and bacon. 昨天晚上我终于发现查理除了 ...

  2. runat="server" 是什么意思?

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...

  3. 开机启动 sh

    我编辑   /etc/init.d/vsftpd    将 nohup java -jar fff.jar &   放在前面 .就执行了

  4. java FLOAT

    System.out.println(""+ 1/2); 得不到0.5,只能得到0. 要想打印出浮点数,必须除数和被除数至少有一个是浮点数,像这样: System.out.prin ...

  5. HP ProLiant DL380 G6 服务器 - 清 BIOS 的方法

    问题 HP ProLiant DL380 G6服务器的BIOS位置在哪里? 如何清BIOS,具体步骤是什么? 解决方案 DL380 G6服务器清BIOS过程分为三步: 1. 为服务器断电(拔掉电源线) ...

  6. Oracle where 0=1 or 1=1

    本文转载自:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html sql where 1=1和 0=1 的作用   where 1 ...

  7. replication across two data centers

    http://andyhan.net/index.php/sys-adm/item/291-hbase-replication http://shitouer.cn/2013/04/hbase-mul ...

  8. SQL 优化案例 1

    create or replace procedure SP_GET_NEWEST_CAPTCHA( v_ACCOUNT_ID in VARCHAR2, --接收短信的手机号 v_Tail_num i ...

  9. ios开发使用lipo命令合并真机库和模拟器库

    在开发ios时,我们经常会遇到编译两套库文件,使用模拟器时链接模拟器库,使用真机时使用真机库,这样操作会对后期的维护带来麻烦,所以Apple提供了一个把多个不同平台的.a库文件合并成一个适用于多平台的 ...

  10. Local declaration of 'XXX' hides instance variable

    今天调试程序遇到这么一个警告! Local declaration of 'XXX' hides instance variable 遇到这种原因,是因为本地变量跟函数参数变量同名.改变其一即可.