[cf1038E][欧拉路]
2 seconds
256 megabytes
standard input
standard output
You are given nn blocks, each of them is of the form [color11 |value|color22 ], where the block can also be flipped to get [color22 |value|color11 ].
A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.
The value of the sequence is defined as the sum of the values of the blocks in this sequence.
Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.
The first line of input contains a single integer nn (1≤n≤1001≤n≤100 ) — the number of given blocks.
Each of the following nn lines describes corresponding block and consists of color1 value and color2 (1≤color1,color2≤4 1≤value≤100000).
Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.
6
2 1 4
1 2 4
3 4 4
2 8 3
3 16 3
1 32 2
63
7
1 100000 1
1 100000 2
1 100000 2
4 50000 3
3 50000 4
4 50000 4
3 50000 3
300000
4
1 1000 1
2 500 2
3 250 3
4 125 4
1000
In the first example, it is possible to form a valid sequence from all blocks.
One of the valid sequences is the following:
[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]
The first block from the input ([2|1|4] →→ [4|1|2]) and second ([1|2|4] →→ [4|2|1]) are flipped.
In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):
[2|100000|1] [1|100000|1] [1|100000|2]
In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
题意:有n个木棒,每个木棒两段有两种颜色,总颜色数<=4,两个木棒颜色一样的一段可以连接起来,得到的新木棒的价值为vi+vj,求拼接后得到的新木棒的最大价值
题解:可以把颜色看成点,木棒看成边,那么由于每种木棒只能使用一次,所以最终结果就是求一条权值和最大的欧拉路(每条边只经过一次的路径)。1)如果图中的度为奇数的点的个数<=2,那么该图是个欧拉图,权值和也就是该图的权值和,2)而本题的点数只有4个,所以如果不是欧拉图,那么度为奇数的点一定是4个,则此时要删掉一条连接两个度为奇数的边,则可以枚举这条要被删掉的边从而得到最大值(注意这个图不一定是连通图,所以要用dfs标记得到所有连通块)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
//#define io_test
#define debug(x,y) cout<<x/2+1<<"#######"<<y/2+1<<"####"<<dp[x]<<endl;
vector<int>g[];
struct edge{
int x;
int y;
int q;
int val;
int nex;
}e[];
int vis[];
int cnt,head[],bk[],d[];
void adde(int x1,int y1,int z1){
e[cnt].x=x1;
e[cnt].y=y1;
e[cnt].val=z1;
e[cnt].q=;
e[cnt].nex=head[x1];
head[x1]=cnt++;
e[cnt].x=y1;
e[cnt].y=x1;
e[cnt].val=z1;
e[cnt].q=;
e[cnt].nex=head[y1];
head[y1]=cnt++;
}
void dfs(int u,int col){
vis[u]=col;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].y;
if(e[i].q)continue;
if(!vis[v]){
dfs(v,col);
}
}
}
int oula(){
int cnt=;
int ans=;
memset(vis,,sizeof(vis));
for(int i=;i<=;i++){
if(!vis[i]){
cnt++;
dfs(i,cnt);
int sum=;
int odd=;
for(int j=;j<=;j++){
if(vis[j]==cnt){
sum+=bk[j];
if(d[j]%)odd++;
}
}
if(odd<=)ans=max(ans,sum);
}
}
return ans;
}
int main()
{
#ifdef io_test
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // io_test
int n;
int sum=;
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
int c1,v1,c2;
scanf("%d%d%d",&c1,&v1,&c2);
adde(c1,c2,v1);
d[c1]++;
d[c2]++;
bk[c2]+=v1;
}
int ss=oula();
if(ss){
printf("%d\n",ss);
}
else{
int ans=;
for(int i=;i<=;i++){
if(d[i]&){
for(int j=head[i];j!=-;j=e[j].nex){
int v=e[j].y;
d[i]--;
d[v]--;
e[j].q=;
e[j^].q=;
ans=max(ans,oula()-e[j].val);
d[i]++;
d[v]++;
e[j].q=;
} }
}
printf("%d\n",ans);
}
return ;
}
[cf1038E][欧拉路]的更多相关文章
- 洛谷P1341 无序字母对[无向图欧拉路]
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- hdu1161 欧拉路
欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...
- UVA10054The Necklace (打印欧拉路)
题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...
- 洛谷 P1341 无序字母对 Label:欧拉路 一笔画
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- hihocoder 1181 欧拉路.二
传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...
- hiho48 : 欧拉路·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的 ...
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
随机推荐
- CPU高速缓存
目录 Code: 物理结构: 缓存行Cache Line 伪共享: 概念: 解决办法: 内存屏障: 理解: 参考: Code: public class Main { static long[][] ...
- Linux查看服务器硬件配置命令
一.查看服务器硬件信息 dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product|Seria ...
- ubuntu14.04 anaconda tensorflow spyder(python3.5) + opencv3
windows上用的tensorflow是依赖于python3.5,因此在linux下也配的3.5 一. 在Anaconda官网上下载Anaconda3-4.0.0-Linux-x86_64.sh文件 ...
- Field笔记
一:时区的转换 1.navie 时间 和 aware 时间 navie 时间:不知道自己的时间表示的是哪个时区: aware 时间:知道自己的时间表示的是哪个时区. 2.pytz 库:用来处理时区的库 ...
- 一键清空Form表单数据
今天在工作项目调试bug当中,遇到这样的需求:页面上的数据太多,一个一个清空太繁琐,所以就采用全部清空的写法: $(':input','#myform').not(':button, :submit, ...
- Java中的CAS实现原理
一.什么是CAS? 在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令. 它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新 ...
- python 获取subprocess进程执行后返回值
test.py #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr= ...
- 三 drf 认证,权限,限流,过滤,排序,分页,异常处理,接口文档,集xadmin的使用
因为接下来的功能中需要使用到登陆功能,所以我们使用django内置admin站点并创建一个管理员. python manage.py createsuperuser 创建管理员以后,访问admin站点 ...
- [转]静态库、动态库,dll文件、lib文件,隐式链接、显式链接
转自:https://blog.csdn.net/dcrmg/article/details/53427181 静态链接.动态链接 静态库和动态库分别应用在静态链接方式和动态链接方式中,所谓静态链接方 ...
- C#实现基于ffmpeg加虹软的人脸识别
关于人脸识别 目前的人脸识别已经相对成熟,有各种收费免费的商业方案和开源方案,其中OpenCV很早就支持了人脸识别,在我选择人脸 识别开发库时,也横向对比了三种库,包括在线识别的百度.开源的OpenC ...