首页 技术 正文
技术 2022年11月11日
0 收藏 547 点赞 2,656 浏览 3417 个字

http://codeforces.com/contest/1038/problem/EE. Maximum Matchingtime limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output

Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.

ExamplesInput

6
2 1 4
1 2 4
3 4 4
2 8 3
3 16 3
1 32 2

Output

63

Input

7
1 100000 1
1 100000 2
1 100000 2
4 50000 3
3 50000 4
4 50000 4
3 50000 3

Output

300000

Input

4
1 1000 1
2 500 2
3 250 3
4 125 4

Output

1000

Note

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902