P2474 [SCOI2008]天平

题目背景

2008四川NOI省选

题目描述

你有n个砝码,均为1克,2克或者3克。你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系。你把其中两个砝码A 和B 放在天平的左边,需要另外选出两个砝码放在天平的右边。问:有多少种选法使得天平的左边重(c1)、一样重(c2)、右边重(c3)?(只有结果保证惟一的选法才统计在内)

输入输出格式

输入格式:

第一行包含三个正整数n,A,B(1<=A,B<=N,A 和B 不相等)。砝码编号

为1~N。以下n行包含重量关系矩阵,其中第i行第j个字符为加号“+”表示砝

码i比砝码j重,减号“-”表示砝码i比砝码j 轻,等号“=”表示砝码i和砝码

j一样重,问号“?”表示二者的关系未知。存在一种情况符合该矩阵。

输出格式:

仅一行,包含三个整数,即c1,c2和c3。

输入输出样例

输入样例#1: 复制

  1. 6 2 5
  2. ?+????
  3. -?+???
  4. ?-????
  5. ????+?
  6. ???-?+
  7. ????-?
输出样例#1: 复制

  1. 1 4 1
输入样例#2: 复制

  1. 14 8 4
  2. ?+???++?????++
  3. -??=?=???????=
  4. ??????????=???
  5. ?=??+?==??????
  6. ???-???-???-??
  7. -=????????????
  8. -??=???=?-+???
  9. ???=+?=???????
  10. ??????????????
  11. ??????+???????
  12. ??=???-????-??
  13. ????+?????+???
  14. -?????????????
  15. -=????????????
输出样例#2: 复制

  1. 18 12 11

说明

4<=n<=50

  1. /*
  2. w[i]<w[j]就连一条i->j的边
  3. w[i]=w[j]就连一条双向边
  4. 跑Tarjan把图缩成有向无环图
  5. 最后入度为0的点的质量就是1,按拓排给其他的赋值为2,3
  6. 由于少考虑了一些情况,导致不合法的情况也会统计到答案中,所以炸了
  7. */
  8. #include<iostream>
  9. #include<cstdio>
  10. #include<queue>
  11. #define maxn 51
  12. using namespace std;
  13. int n,A,B,belong[maxn],wei[maxn],w[maxn],cnt,map[maxn][maxn],du[maxn];
  14. int num,head[maxn],dfn[maxn],low[maxn],st[maxn],top,g,gr[maxn][maxn];
  15. int Num,Head[maxn];
  16. double vis[maxn];
  17. struct node{
  18. int to,pre;
  19. }e[maxn*],E[maxn*];
  20. char s[maxn];
  21. void Insert(int from,int to){
  22. e[++num].to=to;
  23. e[num].pre=head[from];
  24. head[from]=num;
  25. }
  26. void Insert2(int from,int to){
  27. E[++Num].to=to;
  28. E[Num].pre=Head[from];
  29. Head[from]=Num;
  30. }
  31. void Tarjan(int u){
  32. cnt++;
  33. dfn[u]=low[u]=cnt;st[++top]=u;vis[u]=;
  34. for(int i=head[u];i;i=e[i].pre){
  35. int v=e[i].to;
  36. if(!dfn[v]){
  37. Tarjan(v);
  38. low[u]=min(low[u],low[v]);
  39. }
  40. else if(vis[v])low[u]=min(low[u],dfn[v]);
  41. }
  42. if(dfn[u]==low[u]){
  43. g++;
  44. while(st[top]!=u){
  45. int x=st[top];
  46. top--;vis[x]=;
  47. gr[g][++gr[g][]]=x;
  48. belong[x]=g;
  49. }
  50. belong[u]=g;
  51. gr[g][++gr[g][]]=u;
  52. vis[u]=;
  53. top--;
  54. }
  55. }
  56. queue<int>q;
  57. int main(){
  58. freopen("Cola.txt","r",stdin);
  59. scanf("%d%d%d",&n,&A,&B);
  60. for(int i=;i<=n;i++){
  61. scanf("%s",s+);
  62. for(int j=;j<=n;j++){
  63. if(s[j]=='-')map[i][j]=-,map[j][i]=;
  64. if(s[j]=='+')map[i][j]=,map[j][i]=-;
  65. if(s[j]=='=')map[i][j]=,map[j][i]=;
  66. }
  67. }
  68. for(int i=;i<=n;i++){
  69. for(int j=i+;j<=n;j++){
  70. if(map[i][j]==)Insert(j,i);//i>j连一条从j指向i的边
  71. if(map[i][j]==-)Insert(i,j);//i<j连一条从i指向j的边
  72. if(map[i][j]==)Insert(i,j),Insert(j,i);
  73. }
  74. }
  75. for(int i=;i<=n;i++)if(!dfn[i])Tarjan(i);
  76. for(int i=;i<=g;i++){
  77. for(int j=;j<=gr[i][];j++){
  78. int now=gr[i][j];
  79. for(int k=head[now];k;k=e[k].pre){
  80. int to=e[k].to;
  81. if(belong[now]!=belong[to])
  82. Insert2(belong[now],belong[to]),du[belong[to]]++;
  83. }
  84. }
  85. }
  86. for(int i=;i<=g;i++){
  87. if(du[i]==)q.push(i),wei[i]=;
  88. }
  89. while(!q.empty()){
  90. int now=q.front();q.pop();
  91. for(int i=Head[now];i;i=E[i].pre){
  92. int to=E[i].to;
  93. du[to]--;
  94. if(du[to]==){
  95. wei[to]=wei[now]+;
  96. q.push(to);
  97. }
  98. }
  99. }
  100. for(int i=;i<=n;i++)w[i]=wei[belong[i]];
  101. int c1=,c2=,c3=,sum=w[A]+w[B];
  102. for(int i=;i<=n;i++){
  103. if(i==A||i==B)continue;
  104. for(int j=i+;j<=n;j++){
  105. if(j==A||j==B)continue;
  106. if(w[i]+w[j]<sum)c1+=;
  107. if(w[i]+w[j]==sum)c2+=;
  108. if(w[i]+w[j]>sum)c3+=;
  109. }
  110. }
  111. cout<<c1<<' '<<c2<<' '<<c3;
  112. }

20分 拓扑排序

  1. /*
  2. a+b>c+d可以转化为a-c>d-b,可以用差分约束求解
  3. */
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<cstring>
  7. #define maxn 51
  8. using namespace std;
  9. int n,a,b,dx[maxn][maxn],dn[maxn][maxn];
  10. char s[maxn];
  11. int main(){
  12. freopen("Cola.txt","r",stdin);
  13. scanf("%d%d%d",&n,&a,&b);
  14. for(int i=;i<=n;i++){
  15. scanf("%s",s+);
  16. for(int j=;j<=n;j++){
  17. if(i==j||s[j]=='=')dx[i][j]=dn[i][j]=;
  18. else if(s[j]=='+')dn[i][j]=,dx[i][j]=;
  19. else if(s[j]=='-')dn[i][j]=-,dx[i][j]=-;
  20. else dn[i][j]=-,dx[i][j]=;
  21. }
  22. }
  23. for(int k=;k<=n;k++)
  24. for(int i=;i<=n;i++)
  25. for(int j=;j<=n;j++){
  26. if(k==i||k==j||i==j)continue;
  27. dn[i][j]=max(dn[i][j],dn[i][k]+dn[k][j]);
  28. dx[i][j]=min(dx[i][j],dx[i][k]+dx[k][j]);
  29. }
  30. int c1=,c2=,c3=;
  31. for(int i=;i<=n;i++){
  32. if(i==a||i==b)continue;
  33. for(int j=;j<i;j++){
  34. if(j==a||j==b)continue;
  35. if(dn[a][i]>dx[j][b]||dn[b][i]>dx[j][a])c1++;
  36. if(dn[i][a]>dx[b][j]||dn[i][b]>dx[a][j])c3++;
  37. if((dn[a][i]==dx[a][i]&&dn[j][b]==dx[j][b]&&dn[a][i]==dn[j][b]) || (dn[a][j]==dx[a][j]&&dn[i][b]==dx[i][b]&&dn[a][j]==dn[i][b]))c2++;
  38. }
  39. }
  40. printf("%d %d %d",c1,c2,c3);
  41. }

100分 差分约束

洛谷P2474 [SCOI2008]天平的更多相关文章

  1. 洛谷2474 [SCOI2008] 天平 差分约束->枚举

    题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A 和B 放在天平的左边,需要另外选出两个砝码放在天平的右边.问:有多少种 ...

  2. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  3. BZOJ1079或洛谷2476 [SCOI2008]着色方案

    一道记忆化搜索 BZOJ原题链接 洛谷原题链接 发现对于能涂木块数量一样的颜色在本质上是一样的,所以可以直接压在一个状态,而这题的数据很小,直接暴力开\(6\)维. 定义\(f[a][b][c][d] ...

  4. 洛谷P2405 non天平

    题目背景 non最近正在为自己的体重而苦恼,他想称量自己的体重.于是,他找来一个天平与许多砝码. 题目描述 砝码的重量均是n的幂次,n^1.n^2.n^3.n^4.n^5的……non想知道至少要多少个 ...

  5. 洛谷 P2473 [SCOI2008]奖励关 解题报告

    P2473 [SCOI2008]奖励关 题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出\(k\)次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝 ...

  6. 洛谷 P2507 [SCOI2008]配对

    P2507 [SCOI2008]配对 题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值 ...

  7. 洛谷P2507 [SCOI2008]配对

    题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...

  8. 洛谷2473(SCOI2008)奖励关

    题目:https://www.luogu.org/problemnew/show/P2473 因为可不可选此物与之前选过什么物品有关,所以状态可以记录成前面已经选过什么物品. 因为选不选此物与它带来的 ...

  9. 洛谷 P2473 [SCOI2008]奖励关(状压dp+期望)

    题面 luogu 题解 \(n \leq 15\) 状压 \(f[i][S]\)表示第\(i\)轮,吃过的集合为\(S\) 正着转移好像有点复杂 考虑逆推转移(正着转移应该也行) \(f[i][S]\ ...

随机推荐

  1. unity的一些重要技巧(转)【整理他人的东西】

    刚开始学习Unity3D时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇文章,觉得还不错,于是翻译过来和大家共享.原文地址: http://devmag.org ...

  2. Hibernate学习---第九节:Hibernate之hql

    一.Hql 入门 1.实体类: package learn.hibernate.bean; import java.util.Date; import java.util.HashSet; impor ...

  3. python_doc 读写docx文件

    python读写word文档有现成的库可以处理,在这里采用了 python-docx. 首先先安装 pip install python-docx #!/usr/bin/env python # -* ...

  4. 【Codeforces Round #466】E. Cashback DP+ST表

    题意 给定$n$个数,将其划分成若干个连续的子序列,求最小价值,数组价值定义为,数组和减去$\lfloor \frac{k}{c} \rfloor$,$k$为数组长度,$c$为给定数 可以列得朴素方程 ...

  5. hdu-5867 Water problem(水题)

    题目链接: Water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. python 3 serial module install

    /************************************************************************* * python 3 serial module ...

  7. FEC之我见三

    继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节中的x标志位是否扩展了RTP头,RTP协议允许用户自定义的扩展,扩展的字段紧挨上述RTP固定头.RTP扩展投中承载如下信息: 1).当 ...

  8. bzoj 1927 星际竞速 —— 最小费用最大流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 首先注意到这是个DAG: 考虑每个点从哪里来,可以是瞬移来的,也可以是从某个点走过来的 ...

  9. Poj_1045

    这道题难点在于基本物理知识和数学的结合. 得出公式后再code,那就是小菜一碟了. import java.util.Scanner; import java.lang.Math; public cl ...

  10. 解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题

    刚装好SSMS 17.1准备体验,弹出: 一番搜索,普遍办法都是安装VS2015独立shell.删除某个注册表项什么的,没用,首先这个shell我是装了的,然后也没有那个注册表项.我自己尝试过重装sh ...