题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545

题意:有N个村庄, M 个战场; $ 1 <=N,M <= 10^5 $;

其中曹操会从第i个村庄中选出若个人 在x[i]战场为其作战, 同时第i个村庄也会有相同的人数在y[i]战场为袁绍作战;

每个战场i 有对应的重要度w[i],w[i]的值为 0,1,2;

w[i]为2的战场,要求曹操的兵数(从村庄得到的) 严格大于 袁绍的兵的数量;

w[i]为1的战场,曹操的兵数不少于袁绍的兵即可;

w[i]为0的兵,没限制要求;

并且每个村庄派出一个兵有对应的花费c[i], 问要使得曹操在所有的战场士兵人数满足上面的要求,曹操至少花费为多少/

思路:

以贪心的思想很容易想到应该从重要程度为2的村庄开始考虑,如果村庄 i满足w[i]等于2,那么曹操先往该村庄派遣一个士兵;那么由题意知,在y[i]战场 袁绍的并多了1个;怎么办?

是不是曹操就需要往y[i]战场添加士兵呢?

不一定,需要看y[i]战场的重要程度,如果重要程度为0呢~~

所以只需要**递推**到重要程度为0的战场即可;

那怎么考虑花费最少呢?

以每个村庄的**价格**作为边的权值,求出所有w[i]为2的点到最近的w[j] = 0的最短路即可;

但如果直接建边,以重要度为2的战场作为源点spfa到重要度为0的重点,得到的将是曹操为重要度为0的战场的花费;所以反向建边,开始将W[i] = 0的放入队列即可;

spfa解法:

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define pb push_back
#define MK make_pair
#define A first
#define B second
#define clear0 (0xFFFFFFFE)
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define mod 1000000007
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define lowbit(x) (x&(-x))
#define K(x) ((x)*(x))
#define debug(x) printf(" ---- %d\n",x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
inline ll lcm(ll a,ll b){ return a/gcd(a,b)*b; }
template<class T1, class T2> inline void gmax(T1& a, T2 b){ if(a < b) a = b;}
template<class T1, class T2> inline void gmin(T1& a, T2 b){ if(a > b) a = b;}
const int dx[] = {-,,,}, dy[] = {,,,-};
const int maxn = ;
int head[maxn], tot;
ll dist[maxn], vs[maxn];
void init(){
MS0(head);
MSi(dist);
tot = ;
}
struct edge{
int to, w, nxt;
} e[maxn << ]; void ins(int u,int v,int w)
{
e[++tot].nxt = head[u];
e[tot].to = v;
e[tot].w = w;
head[u] = tot;
}
int x[maxn], y[maxn], c[maxn], w[maxn];
int que[maxn];
ll build(int n, int m)
{
int h = , t = ;
rep1(i,,n){
ins(y[i], x[i], c[i]);
if(w[y[i]] == && dist[y[i]] == INF)
que[t++] = y[i], dist[y[i]] = ;
}
while(h < t){
int u = que[h++]; vs[u] = ;
for(int id = head[u]; id; id = e[id].nxt){
int v = e[id].to, w = e[id].w;
if(dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
if(vs[v] == ){
que[t++] = v;
vs[v] = ;
}
}
}
}
ll ans = ;
for(int i = ; i <= m; i++) if(w[i] == ){
if(dist[i] == INF) return -;
ans += dist[i];
}
return ans;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, kase = ;
scanf("%d",&T);
while(T--){
init();
int n, m;
read2(n, m);
rep1(i,,n) read1(x[i]);
rep1(i,,n) read1(y[i]);
rep1(i,,n) read1(c[i]);
rep1(i,,m) read1(w[i]);
printf("Case #%d: %I64d\n",kase++, build(n, m));
}
return ;
}

hdu 5545 The Battle of Guandu spfa最短路的更多相关文章

  1. 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 ...

  2. CDOJ 1220 The Battle of Guandu

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

  3. NOIP2013 华容道 (棋盘建图+spfa最短路)

    #include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...

  4. hdu 4784 Dinner Coming Soon(spfa + 优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4784 思路:建图,对于同一个universe来说,就按题目给的条件相连,对于相邻的universe,连 ...

  5. HDU ACM 1690 Bus System (SPFA)

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. HDU 1224 Free DIY Tour(spfa求最长路+路径输出)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...

  7. HDU 4370 0 or 1(spfa+思维建图+计算最小环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...

  8. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  9. find the safest road HDU杭电1596【Dijkstra || SPFA】

    pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...

随机推荐

  1. 重构18-Replace exception with conditional(条件替代异常)

    重构没有什么出处,是我平时经常使用而总结出来的.欢迎您发表任何改进意见或建议.我相信一定还有其他比较好的重构可以解决类似的问题.  我曾无数次面对的一个代码坏味道就是,使用异常来控制程序流程.您可能会 ...

  2. input[type='submit']input[type='button']button等按钮在低版本的IE下面,去掉黑色边框的问题

    今天做一个tabs效果的时候,发现上面的button在低版本下会出现黑色的边框,很难看,于是我整理了下几个去掉黑色边框的办法: 1.在button的外层嵌套一个div,设置button的border: ...

  3. Http StatuCode说明

    HTTP 200 - 文件被正常的访问 HTTP 302 - 临时重定向 HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导 ...

  4. BZOJ 1050

    1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2593  Solved: 1399[Submit][St ...

  5. 使用Boost asio实现异步的TCP/IP通信

    可以先了解一下Boost asio基本概念,以下是Boost asio实现的异步TCP/IP通信: 服务器: #include "stdafx.h" #include <io ...

  6. jQuer中 height scrollTop

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...

  7. html5的自定义data-*属性和jquery的data()方法的使用示例

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...

  8. ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理

    今天在处理项目中相关警告的时候发现了很多问题,包括各种第三方库中的警告,以及各种乱七八糟的问题  先说说标题中的问题  Category is implementing a method which ...

  9. 20141017--异常语句try-catch

    //try-catch 尝试(try)-抓获(catch) try//尝试,保护起来,使程序出错也能执行 { //确定不会出错时不要用try,当不确定时使用try-catch可以捕获错误, int i ...

  10. tar命令: 对某目录文件打tar包时,排除指定的目录或文件

     如某当前目录存在以下文件或目录: 1.txt2.txt3.txtdir1dir2my2015.tarmy2016.tar 若要对当前目录除1.txt 和dir1.tar外,打包tar 步骤一.建立e ...