其实就是第K短路
学习了以前跳过的启发式搜索(a*算法)
然后需要使用到优先队列。。由于需要用堆实现所以窝萌还是选择STL吧。。
然后priority_queue的话时间复杂度是O(logn),感觉再加上bfs就显得有点大了。。所以题目的数据规模一定得小点才行。。。
A*和bfs相比多了一个估值函数f(x),也就是队列的排序依据,决定出队顺序。。
而又有f(x)=g(x)+h(x)。。
其中g(x)是当前点与起点的距离,可以在bfs过程中维护
然后h(x)就比较玄学了。。它是当前点到终点的距离,本题中直接最短路处理处了精确值。而实际运用中要算出h(x)非常麻烦,所以只需算出估计值,这样的话就只能排除一些比较明显的非最优解,不过也算优化了不少。关于这个h(x)如何取,一般取欧几里得距离(就是横坐标差+纵坐标差嘛。。)或者勾股定理求距离。。感觉蛮暴力的,实际运用中应该可以不用想得那么复杂吧。。。
然后对于这个STL的运用当时觉得并没有什么用也是跳过了。。现在现学现用感觉写出来的代码就很丑了,为了使模板好看点花了不少功夫。。。
再有要吐槽的就是起点和终点重合竟然不是算一次的?又害我WA了好多次qaq
关于这个启发式搜索还是得练练,虽然用得不多
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define _link(x) for(edge *j=_h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define inf 1e9
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define NM 1005
#define nm 200005
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
struct edge{
int t,v;
edge *next;
}e[nm],*h[NM],*_h[NM],*o=e;
void add(int x,int y,int v){
o->t=y;o->v=v;o->next=h[x];h[x]=o++;
}
void _add(int x,int y,int v){
o->t=y;o->v=v;o->next=_h[x];_h[x]=o++;
}
struct node{
int g,t,f;
}N[6*nm],*p=N;
struct cmp{
bool operator()(node*x,node*y){
return x->f==y->f?x->g>y->g:x->f>y->f;
}
};
int n,m,d[NM];
bool v[NM];
int _x,_y,_t,S,T,K,cnt;
void spfa(){
mem(d);mem(v);queue<int >q;
inc(i,1,n)d[i]=inf;
v[T]++;d[T]=0;q.push(T);
while(!q.empty()){
int t=q.front();q.pop();v[t]=false;
_link(t)if(d[j->t]>d[t]+j->v){
d[j->t]=d[t]+j->v;
if(!v[j->t])v[j->t]++,q.push(j->t);
}
}
}
int bfs(){
priority_queue<node*,vector<node*>,cmp>q;
p->g=0;p->f=d[S];p->t=S;K+=S==T;
q.push(p);
while(!q.empty()){
node *k=q.top();q.pop();
if(k->t==T)
if(++cnt==K)return k->f;
link(k->t){
++p;
p->t=j->t;
p->g=k->g+j->v;
p->f=p->g+d[p->t];
q.push(p);
}
}
return -1;
}
int main(){
// freopen("data.in","r",stdin);
while(~scanf("%d%d",&n,&m)){
mem(v);mem(d);mem(e);mem(h);mem(_h);mem(N);p=N;o=e;
inc(i,1,m){
_x=read();_y=read();_t=read();
add(_x,_y,_t);_add(_y,_x,_t);
}
S=read();T=read();K=read();
spfa();
if(d[S]==inf)printf("-1\n");else printf("%d\n",bfs());
}
return 0;
}
You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, ..., bk (1 ≤ b1 < b2 < ... < bk ≤ n) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of .
The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print the maximum possible value of .
4 4 5 2 4 1
3
3 20 199 41 299
19
In the first example you can choose a sequence b = {1, 2}, so the sum is equal to 7 (and that's 3 after taking it modulo 4).
In the second example you can choose a sequence b = {3}.