PAT T1021 Safe Fruit
暴力搜索加剪枝~
看的人好多...更新一下:
这道题的正解应该是求最大团之类的...暴力搜索是拿不到分的,我用了好几种奇怪的技巧用爆搜卡掉了。。。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn];
int c[maxn];
int N,M;
int mincost=1e9;
int cost=;
int flag;
int tle=;
int ans=;
bool visit[maxn];
vector<int> path,tmp;
void dfs (int v,int direction) {
tle++;
if (tle>1e5) return;
if (tmp.size()>path.size()) {
path=tmp;
mincost=cost;
}
else if (tmp.size()==path.size()&&cost<mincost) {
path=tmp;
mincost=cost;
}
if (direction==) {
v++;
while (v<=) {
flag=;
if (c[v]==) {
v++;
continue;
}
for (int j=;j<tmp.size();j++)
if (g[v][tmp[j]]==) {
flag++;
break;
}
if (flag==) {
tmp.push_back(v);
visit[v]=true;
cost+=c[v];
dfs (v,);
tmp.pop_back();
visit[v]=false;
cost-=c[v];
}
v++;
}
}
else {
v--;
while (v>=) {
flag=;
if (c[v]==) {
v--;
continue;
}
for (int j=;j<tmp.size();j++)
if (g[v][tmp[j]]==) {
flag++;
break;
}
if (flag==) {
tmp.push_back(v);
visit[v]=true;
cost+=c[v];
dfs (v,);
tmp.pop_back();
visit[v]=false;
cost-=c[v];
}
v--;
}
}
}
int main () {
scanf ("%d %d",&M,&N);
int u,v;
for (int i=;i<M;i++)
scanf ("%d %d",&u,&v),g[u][v]=g[v][u]=;
for (int i=;i<=N;i++) {
scanf ("%d %d",&u,&v);
c[u]=v;
if (v>) ans++;
}
if (ans!=) dfs (,);
else dfs (,);
printf ("%d\n",path.size());
sort (path.begin(),path.end());
for (int i=;i<path.size();i++) {
if (i!=) printf (" ");
printf ("%03d",path[i]);
}
printf ("\n%d",mincost);
return ;
}
PAT T1021 Safe Fruit的更多相关文章
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- Warning: strftime(): It is not safe to rely on the system's timezone settings.
当运行程序时,会出现如下警告: Warning: strftime(): It is not safe to rely on the system's timezone settings. You a ...
- The Safe Navigation Operator (&.) in Ruby
The most interesting addition to Ruby 2.3.0 is the Safe Navigation Operator(&.). A similar opera ...
- 【转】php Thread Safe(线程安全)和None Thread Safe(NTS,非 线程安全)之分
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍. ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PAT 1041. 考试座位号(15)
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- PAT 1040. 有几个PAT(25)
字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...
- PAT 1032. 挖掘机技术哪家强(20)
为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...
随机推荐
- Spring Cloud netflix feign【服务间通信】
一.简介 1,进程间通讯的本质是交换消息 2,服务间通信的两种方式 (1)RESTFul风格 (2)RPC风格 (3)两种风格的比较 3.基于RESTFul风格服务调用模型 4.基于Spring Cl ...
- vscode生成文件头注释(python)
文件→首选项→用户代码片段→选python 在大括号内添加如下内容: "Print infomation": { "prefix": "prelog& ...
- 启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting 解决方法: vim /etc/m ...
- GO学习之 安装Go语言及搭建Go语言开发环境
一.下载 1.下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ 2.版本的选择 Windows ...
- webpack初学踩坑记
注意事项: 1. webpack不用装在全局环境下,在哪个项目中使用,就安装在该项目下即可 1. 问题一:npm init 初始化一个项目后,添加webpack.config.js文件,在该项目中通过 ...
- 《macOS 使用手册》之远程控制
https://support.apple.com/zh-cn/guide/mac-help/mchlp1066/mac 选择版本: macOS Catalina 10.15 macOS Mojave ...
- vue 实现简单的富文本编辑
安装:npm install vue-quill-editor --save 安装Vue-Quill-Editor需要依赖: npm install quill --save 在入口文件main.js ...
- es 的搜索
kibana语言查询 q指定查询语句 df默认字段,不指定时 sort排序/from和size用于分页 profile可以查看查询是如何倍执行的 查询title的值等于2012 的数据 GET /mo ...
- Git添加和克隆远程库
首先我们得有一个GitHub账号,然后把当前电脑的SSH Key添加到GitHub上面 第1步:创建SSH Key.在用户主目录下(可用 “cd ~”进入用户主目录),看看有没有.ssh目录,如果有, ...
- 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集
import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...