直接爆搜的复杂度是2^n,对于n<=40的数据过不了。

考虑优化一下。

发现如果走了一个点后,以后是不可能再经过与它相邻的点的,因为这样走显然不如直接走那个与它相邻的点。

这样每走一步就可以删掉d[x]个点,d[x]为x的度数。

这样做的复杂度O(n)=k*O(n-k)

在k=3的时候取到最大是,约等于3^13,可以通过。

#include<bits/stdc++.h>
#define N 110000
#define L 100000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
char ch=0;
int x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
struct edge{int to,nxt;}e[N*2];
int num,head[N];
inline void add(int x,int y){e[++num]={y,head[x]};head[x]=num;}
int n,m,ans=inf,w[N],vis[N];
void dfs(int x,int s)
{
if(++vis[x]==1)s+=w[x];
for(int i=head[x];i!=-1;i=e[i].nxt){int to=e[i].to;if(++vis[to]==1)s+=w[to];}
if(x==n)
{
ans=min(ans,s);
vis[x]--;for(int i=head[x];i!=-1;i=e[i].nxt)vis[e[i].to]--;return; }
for(int i=head[x];i!=-1;i=e[i].nxt){int to=e[i].to;if(vis[to]==1)dfs(to,s);}
vis[x]--;for(int i=head[x];i!=-1;i=e[i].nxt)vis[e[i].to]--;
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++)w[i]=read();
num=-1;memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
add(x,y);add(y,x);
}
dfs(1,0);printf("%d\n",ans);
return 0;
}

Routing a Marathon Race的更多相关文章

  1. ICPC Asia Regional 2015 Japan.Routing a Marathon Race(DFS)

    vjudge \(Description\) 给定一张\(n\)个点\(m\)条边的无向图,每个点有一个权值.求一条从\(1\)到\(n\)的路径,使得代价最小,输出最小代价. 一条路径的代价定义为, ...

  2. CodeForces 404 Marathon ( 浮点数取模 -- 模拟 )

    B. Marathon time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  3. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  4. Codeforces Round #237 (Div. 2) B题模拟题

    链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...

  5. Codeforces Round #237 (Div. 2)

    链接 A. Valera and X time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inp ...

  6. 可扩展的Web系统和分布式系统(Scalable Web Architecture and Distributed Systems)

    Open source software has become a fundamental building block for some of the biggest websites. And a ...

  7. Scalable Web Architecture and Distributed Systems

    转自:http://aosabook.org/en/distsys.html Scalable Web Architecture and Distributed Systems Kate Matsud ...

  8. 【原创】大数据基础之Marathon(1)简介、安装、使用

    marathon 1.6.322 官方:https://mesosphere.github.io/marathon/ 一 简介 Marathon is a production-grade conta ...

  9. Meandering Through the Maze of MFC Message and Command Routing MFC消息路由机制分析

    Meandering Through the Maze of MFC Message and Command Routing Paul DiLascia Paul DiLascia is a free ...

随机推荐

  1. ZOJ 3593 One Person Game(ExGcd + 最优解)题解

    思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|.显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| ...

  2. P3338 [ZJOI2014]力

    思路 颓柿子的题目 要求求这样的一个式子 \[ F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2} ...

  3. 题解——洛谷P2827 NOIP提高组 2016 蚯蚓

    队列模拟 详细题解待填坑 #include <cstdio> #include <algorithm> #include <queue> #include < ...

  4. Linux 压缩、解压命令使用

    tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,这里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:(个人理解也就是打包) -x : 解压缩压缩档案 ...

  5. website for .Net Core

    5 Ways to Build Routing in ASP.NET Core Bundling in .NET Core MVC Applications with BundlerMinifier. ...

  6. python学习 day09打卡 初识函数

    本节内容: 1.什么是函数 2.函数定义,函数名,函数体及函数的调用 3.函数的返回值 4.函数的参数 一.什么是函数 函数:对代码块和功能的封装和定义 定义一个事情或者功能.等到需要的时候直接去用, ...

  7. jenkins+ant+jmeter自动化性能测试平台

    jenkins+ant+jmeter自动化性能测试平台 Jmeter是性能测试的工具,java编写.开源,小巧方便,可以图形界面运行也可以在命令行下运行.网上已经有人使用ant来运行,http://w ...

  8. layer 弹出层 回调函数调用 弹出层页面 函数

    1.项目中用到layer 弹出层,定义一个公用的窗口,问题来了窗口弹出来了,如何保存页面上的数据呢?疯狂百度之后,有了结果,赶紧记下. 2.自己定义的公共页面方法: layuiWindow: func ...

  9. 前端如何应对笔试算法题?(用node编程)

    用nodeJs写算法题 咱们前端使用算法的地方不多,但是为了校招笔试,不得不针对算法题去练习呀! 好不容易下定决心 攻克算法题.发现js并不能像c语言一样自建输入输出流.只能回去学习c语言了吗?其实不 ...

  10. Vue--vux组件库

    各种组件demo源码~ https://doc.vux.li/zh-CN/