CDOJ UESTC 1220 The Battle of Guandu
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的更多相关文章
- CDOJ 1220 The Battle of Guandu
The Battle of Guandu Time Limit: 6000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Oth ...
- 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 ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
题目传送门 题意:问n长度的序列,找出长度m的上升子序列的方案数. 分析:这个问题就是问:dp[i][j] = sum (dp[i-1][k]) (1 <= k <= n, a[k] &l ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...
- 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]. ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- 2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)
赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路…… 因为无法提交,不确定是否正确..嗯..有错希望指出,谢谢... 嗯..已经A了..提交地址http://acm.uestc.ed ...
- CDOJ 1217 The Battle of Chibi
The Battle of Chibi Time Limit: 6000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
随机推荐
- 邮件发布google blogger 博客
<?php $to = "@gmail.com";$subject = "Test mail";$message = "Hello! This ...
- leetcode415---字符串大数相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- form表单中的带有name的标签直接转JSON
1. 将如下内容引入页面:比如说新建一个 a.js,然后在页面中引入a.js//将form表单序列化成json格式 $.fn.serializeObject = function () { var o ...
- 第十五节,基本数据类型,元组tuple
元组和列表的区别 元组和列表几乎是一样的 不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的) 基本操作: 索引 切片 循环 长度 包 ...
- nefu 115 斐波那契的整除
Description 已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13, ...
- mynotebook
www.linux.org/threads/beats-audio-on-linux.4443/ askubuntu.com/questions/303775/envy-15-beats-audio- ...
- lepus bug
------------------------------------------------BUG fix-------------------------------------------- ...
- VS2010中安装AjaxControlToolkit
原文地址:http://www.asp.net/ajaxlibrary/act.ashx 第一步 下载Ajax Control Toolkit 进入网址http://ajaxcontroltoolki ...
- win8上cmder文字重叠问题
1.用过ubuntu上的bash,zsh后发现win自带的cmd弱爆了,在网上搜索后找到了代替品cmder,下载安装后好发现中文错位的问题, 状况如下: 修复方法:把设置里面的Monospace选项勾 ...
- Android OpenGL ES(一)OpenGL ES介绍
在学习Android OpenGL ES开发之前,你必须具备Java 语言开发经验和一些Android开发的基本知识,但并不需要有图形开发的经验,本教程也会涉及到一些基本的线性几何知识,如矢量,矩阵运 ...