# NOI 2387:Til the Cows Come Home

#### 题目

http://bailian.openjudge.cn/practice/2387/

#### 源码

``````//
//  2387.cpp
//  test
//
//  Created by bytedance on 2020/9/8.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#define inf 99999999
using namespace std;
struct Edge{
int from,to,dis;
Edge(int from,int to,int dis):from(from),to(to),dis(dis){};
bool operator < (const Edge &e) const{
return e.dis<dis;
}
};

int n, t;

int main(){
cin >> t >> n;
vector<vector<Edge>> edges(n+1,vector<Edge>());
priority_queue<Edge> q;
vector<int> d(n+1,inf);
vector<bool> done(n+1);
while(t--){
int a,b,c;
cin >> a>>b>>c;
Edge e(a,b,c);
edges[a].push_back(e);
Edge e2(b,a,c);
edges[b].push_back(e2);
}
//边读入ok，初始化距离数组
q.push(Edge(1,1,0));
d[1]=0;
while(!q.empty()){
int node  = q.top().to;q.pop();
if(done[node])continue;
done[node] = true;
for(int i = 0;i < edges[node].size();++i){
Edge e = edges[node][i];
if(d[e.to]>d[node]+e.dis){
d[e.to] = d[node]+e.dis;
q.push(Edge(1,e.to,d[e.to]));
}
}
}
cout << d[n];
return 0;
}
``````

# NOI 1840:Eqs

#### 题目

http://bailian.openjudge.cn/practice/1840/

#### 源码

``````//
//  1840.cpp
//  test
//
//  Created by bytedance on 2020/9/8.
//

#include <stdio.h>
#include <iostream>
#include <map>
#include <math.h>
using namespace std;

int main(){
int cnt = 0;
map<int,int> m;
int a1,a2,a3,a4,a5;
cin >> a1>>a2>>a3>>a4>>a5;
for(int i = -50;i <= 50;++i){
int res1 = i*i*i*a1;
for(int j = -50;j <= 50;++j){
int res = res1+j*j*j*a2;
if(i==0||j==0)continue;
if(m.find(res)!=m.end()){
m[res]++;
}else{
m[res] = 1;
}

}
}
//    for(map<int,int>::iterator it = m.begin();it!=m.end();++it){
//        cout <<it->first<<" "<<it->second<<endl;
//    }

for(int i = -50;i <= 50;++i){
for(int j = -50;j <=50;++j){
for(int k = -50;k <= 50;++k){
if(i==0||j==0||k==0)continue;
int res = i*i*i*a3+j*j*j*a4+k*k*k*a5;
res = -res;
if(m.find(res)!=m.end()){
cnt+=m[res];
}
}
}
}
cout << cnt;
return 0;
}
``````

# POJ 3126.Prime Path 素数筛+双向BFS

#### 题目

http://poj.org/problem?id=3126

#### 源码

``````//
//  3126.cpp
//  test
//
//  Created by bytedance on 2020/9/8.
//
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
bool pre[10000]={false};
vector<string> p;
int main(){
int time;
cin >> time;

string s,e;
for(int i = 2;i <= 9999;++i){
for(int j = i*2;j<=9999;j+=i)pre[j]=true;
}
for(int i = 1000;i<=9999;++i){
if(!pre[i])p.push_back(to_string(i));
}
while(time--){
cin >> s>>e;
//素数集合构造完毕
int idx = 0;
queue<string> q[2];
map<string,bool> vis[2];
q[0].push(s);
q[1].push(e);
vis[0][s]=true;
vis[1][e]=true;
int cnt = 0;
bool f = true;
while(!q[0].empty()&&!q[1].empty()&&f){
idx=idx^1;
int sz = q[idx].size();
while(sz--&&f){
string crt = q[idx].front();
q[idx].pop();
if(vis[idx^1][crt]==true){ cout<< cnt; f=false;}
for(int i = 0;i < 4&&f;++i){
char c = crt[i];
for(int j = '0';j <='9'&&f;++j){
crt[i] = j;
if(j==c||vis[idx][crt]||find(p.begin(),p.end(),crt)==p.end())continue;
q[idx].push(crt);
vis[idx][crt]=true;
}
crt[i] = c;
}
}
cnt++;
}
}
return 0;
}

``````

# NOI 2355:Railway tickets dp

#### 题目

http://bailian.openjudge.cn/practice/2355/

#### 思路

dp[i]表示到i的最短路径，直接 dp 求解，i 从 1到n遍历求解dp[i],只会用到 dp[k] k < i

#### 源码

``````//
//  2355.cpp
//  test
//
//  Created by bytedance on 2020/9/7.
//

#include <iostream>
#include <vector>
#define INF 999999999999
using namespace std;

long c1,c2,c3,l1,l2,l3;
int n,s,e;
long pre[10005]={0};
int main(){
cin >>l1>>l2>>l3;
cin >> c1>>c2>>c3;
cin >> n;
cin >>s>>e;
if(s>e)swap(s,e);
for(int i = 2;i <= n;++i){
cin >> pre[i];
}
vector<long>d(n+1,INF);
d[s] = 0;
for(int i = s+1;i <= n;++i){
for(int j = i-1;j>=s;--j){
long dis = pre[i]-pre[j];
if(dis<=l1)d[i]=min(d[i],d[j]+c1);
else if(dis<=l2)d[i] = min(d[i],d[j]+c2);
else if(dis<=l3)d[i] = min(d[i],d[j]+c3);
else break;
}
}
cout<<d[e];
return 0;
}

``````