直接爆搜的复杂度是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. Python 装饰器实例

    retry 偶然看到一篇文章,想到了前几天的一个需求,git pull性能不稳,需要加入重试机制,正好这个装饰器的实例符合这样的场景. # coding:utf-8 import time impor ...

  2. 【做题】CF285E. Positions in Permutations——dp+容斥

    题意:求所有长度为\(n\)的排列\(p\)中,有多少个满足:对于所有\(i \,(1 \leq i \leq n)\),其中恰好有\(k\)个满足\(|p_i - i| = 1\).答案对\(10^ ...

  3. (转)renren-fast解读(一)

    (二期)8.renren-fast项目解读(一) [课程八]预防xss...注入.xmind0.2MB [课程八预习]开...解读.xmind0.5MB 课程八_日志处理与...模块.xmind0.2 ...

  4. LOJ121 「离线可过」动态图连通性

    思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...

  5. p1110 报表统计(FHQ-TREAP/TREAP)

    平衡树的裸题,操作都相当简单 写了一个FHQ,但是奈何常数太大,实在过不去 最后写了一个Treap开O2水过 TREAP代码 #include <cstdio> #include < ...

  6. 题解——loj6280 数列分块入门4 (分块)

    分块维护一个区间和 然后记得更新的时候左边角块的tag不要打错到右边角块 #include <cstdio> #include <algorithm> #include < ...

  7. Nand flash code

    (1)流水灯   1>我们来看原理图                                2>datasheet                             3> ...

  8. EM算法——Expectation-Maximization

    最大似然估计 一个栗子:假如去赌场,但是不知道能不能赚钱,你就在门口堵着出来一个人就问一个赚了还是赔了,如果问了5个人都说赚了,那么你就会认为,赚钱的概率肯定是非常大的. 已知:(1)样本服从分部的模 ...

  9. shell中的${},##, %% , :- ,:+, ? 的使用

    假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个/ 及其左边的字符串:dir1/dir2 ...

  10. React native 之android的图标和启动图片

    哎哎呀呀,上篇说到了react native的IOS的图标和启动图片的设置,其实最主要的是尺寸!相应的尺寸设定好了以后就不会报错了! ok~这篇说的是React native的android的图标和启 ...