【NOIP2013模拟】四叶草魔杖

Time Limits: 1000 ms Memory Limits: 131072 KB

Description
魔杖护法Freda融合了四件武器,于是魔杖顶端缓缓地生出了一棵四叶草,四片叶子焕发着淡淡的七色光。圣剑护法rainbow取出了一个圆盘,圆盘上镶嵌着N颗宝石,编号为0~N-1。第i颗宝石的能量是Ai。如果Ai>0,表示这颗宝石能量过高,需要把Ai的能量传给其他宝石;如果Ai<0,表示这颗宝石的能量过低,需要从其他宝石处获取-Ai的能量。保证sigma(Ai)=0。只有当所有宝石的能量均相同时,把四叶草魔杖插入圆盘中央,才能开启超自然之界的通道。

不过,只有M对宝石之间可以互相传递能量,其中第i对宝石之间无论传递多少能量,都要花费Ti的代价。探险队员们想知道,最少需要花费多少代价才能使所有宝石的能量都相同?

Input
第一行两个整数N、M。

第二行N个整数Ai。

接下来M行每行三个整数pi,qi,Ti,表示在编号pi和qi的宝石之间传递能量需要花费Ti的代价。数据保证每对pi、qi最多出现一次。

Output
输出一个整数表示答案。无解输出Impossible。

Sample Input
3 3
50 -20 -30
0 1 10
1 2 20
0 2 100

Sample Output
30

Data Constraint
对于50%的数据,2<=N<=8。
对于100%的数据,2<=N<16,0<=M<=N*(N-1)/2,0<=pi,qi< N,-1000<=Ai<=1000,0<=Ti<=1000, ∑ni=1Ai \sum_{i=1}^n Ai=0。


解题思路
我们可以找到几个集合,每个集合点的权值和为0。
那么我们可以将每个集合求出最小生成树的距离,标记到一个数组f里,用二进制状态压缩储存,对于每两个编号,x,y,用f[x]+f[y]更新f[x xor y],存入x xor y。

var father:array[0..16]of shortint;f:array[0..65536] of longint;v:array[0..16]of longint;a:array[0..1000,1..3]of longint;t:array[0..1000000]of longint;i,n,m,top,j:longint;
function get(x:longint):longint;
beginif father[x]=x then exit(x);father[x]:=get(father[x]);exit(father[x]);
end;
procedure qsort(h,t:longint);var l,r,m:longint;
beginl:=h; r:=t;m:=a[(h+t)div 2,3];repeatwhile a[l,3]<m do inc(l);while a[r,3]>m do dec(r);if l<=r thenbegina[0]:=a[l]; a[l]:=a[r]; a[r]:=a[0];inc(l); dec(r);end;until l>r;if h<r then qsort(h,r);if l<t then qsort(l,t);
end;
function check(what,tot:longint):longint;var o,x,y,num,dis:longint;
beginif tot=1 then exit(0);for o:=1 to n do father[o]:=o;num:=0; dis:=0;for o:=1 to m do  //做最小生成树beginif ((1 shl (a[o,1]-1))and what)>0 thenif ((1 shl (a[o,2]-1))and what)>0 thenbeginx:=get(a[o,1]); y:=get(a[o,2]);if x<>y thenbeginfather[x]:=father[y];inc(num); inc(dis,a[o,3]);if num=tot-1 then exit(dis);end;end;end;exit(maxlongint);
end;
procedure find(x,tot,what,sum:longint);  //寻找权值和为0的点集var o:longint;
beginif sum=0 thenbeginf[what]:=check(what,tot);  //检查集合是否连通if f[what]<maxlongint thenbegininc(top);t[top]:=what;end;end;if x>n then exit;for o:=x to n do find(o+1,tot+1,what+1 shl (o-1),sum+v[o]);  //参数传递用二进制状态压缩
end;
procedure print;var x,y:longint;
begini:=0;while i<top dobegininc(i);for j:=i+1 to top dobeginx:=f[t[i]]+f[t[j]];  //更新y:=t[i] xor t[j];if x<f[y] thenbegininc(top);t[top]:=y; f[y]:=x;  //存入队列end;end;end;if f[1 shl n-1]=maxlongint then writeln('Impossible')else writeln(f[1 shl n-1]);
end;
beginread(n,m);for i:=1 to n do read(v[i]);for i:=1 to m dobeginread(a[i,1],a[i,2],a[i,3]);inc(a[i,1]); inc(a[i,2]);end;qsort(1,m);for i:=0 to 1 shl n do f[i]:=maxlongint;find(1,0,0,0);print;
end.

【NOIP2013模拟】四叶草魔杖相关推荐

  1. JZOJ 3461. 【NOIP2013模拟联考5】小麦亩产一千八(kela)

    3461. [NOIP2013模拟联考5]小麦亩产一千八(kela) (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Det ...

  2. JZOJ 3518. 【NOIP2013模拟11.6A组】进化序列(evolve)

    3518. [NOIP2013模拟11.6A组]进化序列(evolve) (File IO): input:evolve.in output:evolve.out Time Limits: 1000 ...

  3. 【NOIP2013模拟】Freda的传呼机

    [NOIP2013模拟]Freda的传呼机 Time Limits: 100 ms Memory Limits: 131072 KB Description 为了 随时 与 rainbow快速交流, ...

  4. jzoj 3461. 【NOIP2013模拟联考5】小麦亩产一千八(math)

    3461. [NOIP2013模拟联考5]小麦亩产一千八 Description "有了金坷垃,肥料一袋能顶两袋撒,小麦亩产一千八,吸收两米下的氮磷钾--",话说HYSBZ(Hen ...

  5. JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

    3388. [NOIP2013模拟]绿豆蛙的归宿 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limi ...

  6. tyvj 2054 [Nescafé29]四叶草魔杖

    http://www.elijahqi.win/archives/1395 背景 陶醉在彩虹光芒笼罩的美景之中,探险队员们不知不觉已经穿过了七色虹,到达了目的地,面前出现了一座城堡和小溪田园,城堡前的 ...

  7. [tyvj2054] 四叶草魔杖 (最小生成树 状压dp)

    传送门 Background 陶醉在彩虹光芒笼罩的美景之中,探险队员们不知不觉已经穿过了七色虹,到达了目的地,面前出现了一座城堡和小溪田园,城堡前的木牌上写着"Poetic Island&q ...

  8. TYVJ 2054 [Nescafé29]四叶草魔杖 最小生成树 状态压缩/背包DP

    $ \rightarrow $ 戳我进TYVJ原题 [Nescafé29]四叶草魔杖 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背 ...

  9. 【NOIP2013模拟】黑魔法师之门

    题目描述 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Violet星球. ...

最新文章

  1. 找规律 百度之星资格赛 1001 大搬家
  2. 自定义图片,实现透明度动态变化
  3. 工业轨式 1-16路 4-20MA 模拟量光端机产品介绍
  4. Redis:从应用到底层,都在这儿了!
  5. 检查丢失的软件包并安装它们的优雅方法?
  6. java关闭服务_实现优雅地关闭Docker中的java服务
  7. java 泛型的类型擦除和桥方法
  8. 现美团架构师分享过去三年的面试感悟—Java程序员应该具备的技能
  9. 奶制品生产与销售matlab,奶制品生产和销售
  10. Linux 内存管理篇(1)内存寻址
  11. 传Snapchat母公司Snap拟于3月在纽交所IPO上市
  12. webshell 常见 Bypass waf 技巧总结
  13. 遭遇腾讯SOSO爬虫攻击
  14. 打击标题党 详述清风算法
  15. Ty-Mysql函数笔记
  16. 产品经理游戏公司的产品经理
  17. 80/20法则:少工作,多赚钱,多享受生活
  18. QQ日志搬家工具 1.0
  19. jswebApi 03
  20. 【thinkPhP】96PHPCN音乐电台(支持收听酷狗收费音乐)

热门文章

  1. Rust 错误:use of moved value: `xxxx` value used here after moverustc(E0382)
  2. 2023年MathorCup数学建模B题思路 - 城市轨道交通列车时刻表优化问题
  3. 如何在本地部署小幺鸡(小幺鸡简单好用的接口管理工具)
  4. linux 非root用户mount,linux – 选项“user”适用于mount,而不适用于umount
  5. 形象易懂讲解算法II——压缩感知
  6. spring中packagesToScan
  7. 区块链游戏崩盘的前兆有哪些?一文为你分享。
  8. SQL游戏行业实战案例2:玩家等级(union、分组、排序)
  9. JAVA操作excel生成报表
  10. [31期] 分享是一种理念