1617: 刘备闯三国之汉中之战

Time Limit: 1000 MS  Memory Limit: 128 MB
Submit: 6  Solved: 5
[Submit][Status][Web Board]

Description

刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县,西汉中山靖王刘胜的后代。刘备一生极具传奇色彩,早年颠沛流离、备尝艰辛最终却凭借自己的谋略终成一方霸主。那么在那个风云激荡的年代,刘备又是如何从一个卖草鞋的小人物一步一步成为蜀汉的开国皇帝呢?让我们一起拨开历史的迷雾,还原一个真实的刘备。

建安二十二年,刘备与曹操争夺汉中,展开了一场大战。

刘备的军队有n(1<=n<=200000)个营寨,营寨之间有m条道路相连(1<=m<=200000),刘备想在一些营寨上放置一些装备。由于装备有限且不同的道路重要程度不一样,有些道路连接的2个营寨都要放装备,有的道路只要在其连接的两个营寨之一放置装备即可,有的道路连接的两个营寨都不需要放置装备。

如何在满足条件下,放置最少的装备又是一个头疼的问题。当然,由于之前的出色发挥,现在已经晋升为大内总管的你,是时候证明你自己配得上这个职位了。

Input

第一行包含两个整数n,m。

接下来m行,每行有3个整数a,b,c.

a,b表示营寨a和b之间有一条道路(1<=a,b<=n);

c为0,或1或2。表示这条道路两边的营寨应该恰好选择c个放置装备。

Output

如果存在满足要求的方案,输出最少需要放置装备的数量。

否则输出impossible。

Sample Input

样例一:
4 4
1 2 2
2 3 1
3 4 1
4 1 2

样例二:
5 5
1 2 1
2 3 1
2 4 1
2 5 1
4 5 1

Sample Output

样例一:
3

样例二:
impossible

HINT

 

Source

[Submit][Status][Web Board]

题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1617

题目大意:

  N个点,M条边,每条边都有一个要求军械库数量C(0 1 2),表示这条边的两个端点的军械库数量和为C,且每个端点最多放1个军械库。

  问是否能够满足军械库放置要求,能满足的话输出最小的军械库数量。

题目思路:

  【BFS+染色】

  因为每个点只能有或没有军械库。将军械库视为标记。首先由于是无向图所以存在若干联通块。

  可以枚举每个联通块的其中一个点有还是没有标记,并用这个点去拓展这个点的联通块并01染色(这个点所能到达的所有点)

  初始点标记为0需要的标记数为sum0,初始点为1的标记数为sum1,选取0 或1 能够满足的加到答案,如果0 1染色都能够满足图的要求就取min。

  在BFS的过程中只要有一个节点同时不满足0和1染色则无解。

  每个点只会被走过一次,所以是O(N)的。

 /****************************************************

     Author : Coolxxx
Copyright 2017 by Coolxxx. All rights reserved.
BLOG : http://blog.csdn.net/u010568270 ****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=1e-;
const int J=;
const int MOD=;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int q[N],last[N];
int mark[N][];
bool u[N];
struct xxx
{
int next,to,d;
}a[N+N];
void add(int x,int y,int z)
{
a[++lll].to=y;
a[lll].d=z;
a[lll].next=last[x];
last[x]=lll;
}
bool spfa(int s)
{
int i,l=,r=,now,to,sumz=,sumo=;
bool z=,o=;
q[]=s;mark[s][]=,mark[s][]=;u[s]=;
while(l!=r)
{
now=q[l=(l+)%N];
sumz+=mark[now][];sumo+=mark[now][];
for(i=last[now];i;i=a[i].next)
{
to=a[i].to;
if(mark[to][]==-)mark[to][]=a[i].d-mark[now][];
if(mark[to][]==-)mark[to][]=a[i].d-mark[now][];
if(mark[now][]+mark[to][]!=a[i].d || mark[to][]< || mark[to][]>)z=;
if(mark[now][]+mark[to][]!=a[i].d || mark[to][]< || mark[to][]>)o=;
if(!u[to])
{
u[to]=;
q[r=(r+)%N]=to;
}
}
if(z && o)break;
}
if(z && o)return ;
if(!z && o)ans+=sumz;
else if(z && !o)ans+=sumo;
else if(!z && !o)ans+=min(sumz,sumo);
return ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
mem(u,);mem(mark,-);
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
ans=;
for(i=;i<=n;i++)
{
if(u[i])continue;
if(!spfa(i))break;
}
if(i<=n)puts("impossible");
else printf("%d\n",ans);
}
return ;
}
/*
// //
*/

最新文章

  1. (翻译)Angular.js为什么如此火呢?
  2. SourceTree 免登录跳过初始设置 - 转
  3. squid清除缓存
  4. canvas之万花筒
  5. [Matlab.Matrix] reshape
  6. Javascript位置 body之前、后执行顺序
  7. ubuntu 彻底删除软件包
  8. CustomTabBarViewController
  9. OpenCV学习目录(持续更新)
  10. Children of the Candy Corn (bfs+dfs)
  11. OCR怎么能离开扫描仪呢?
  12. IOS中http请求使用cookie
  13. 利用sql里的xpath生成表格
  14. JavaWeb(一)之细说Servlet
  15. AspNet Core Api Restful 实现微服务之旅 (一)
  16. UVA 1626 Brackets sequence 区间DP
  17. linux学习之命令的排列、替换和别名--2019-04-23
  18. Wpf DataGrid 自动滚动到最后一行
  19. RoR - More Active Record
  20. php 日期格式转换万能公式

热门文章

  1. [第一波模拟\day1\T2]{分班}(divide.cpp)
  2. [WPF自定义控件]使用WindowChrome自定义Window Style
  3. 【02】SASS与SCSS
  4. URAL 2040 Palindromes and Super Abilities 2
  5. POJ3246-Balanced Lineup,好经典的题,做法和HDU-I hate it 一样~~
  6. 机器学习基础-Logistic回归2
  7. [codeforces538F]A Heap of Heaps
  8. BZOJ3027 - [CEOI2004]Sweet
  9. hdu5608:function
  10. msp430项目编程11