题解
2024-08-26 20:04:57
发布于:广东
1阅读
0回复
0点赞
题目比较迷惑,反正不知道怎么就AC了
//#include <cjdst.h>
#include <vector>
#include <memory.h>
#include <queue>
#include <iostream>
#include <cstdio>
const int A = 'A' - 1;
void read(int &);
void read(char &);
void read(int &x){
char c = getchar();
x = 0;
int f = 1;
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
}
void read(char &c){
c = getchar();
while(!isalpha(c)) c = getchar();
}
using namespace std;
struct node{
int to, len;
bool operator < (const node &b) const{
return len > b.len;
}
};
vector <node> v[21];
int dis[21];
bool vis[21];
int n, m, z;
char k, t, x, y;
int dijkstra(){
memset(dis, 63, sizeof(dis));
dis[k] = 0;
priority_queue <node> q;
q.push({k, 0});
while(!q.empty()){
node head = q.top();
q.pop();
if(vis[head.to]) continue;
vis[head.to] = 1;
for(node it:v[head.to]){
if(head.len + it.len < dis[it.to]){
dis[it.to] = head.len + it.len;
q.push({it.to, dis[it.to]});
}
}
}
return dis[t];
}
void push(vector <node> &v, node a){
for(int i = 0; i < v.size(); i++){
if(v[i].to == a.to){
v[i].len = max(v[i].len, a.len);
return;
}
}
v.push_back(a);
}
int main(){
read(n), read(m);
for(int i = 1; i < n; i++){
read(z);
v[i].push_back({i + 1, z}), v[i + 1].push_back({i, z});
}
read(z);
v[n].push_back({1, z}), v[1].push_back({n, z});
for(int i = 1; i <= m; i++){
read(x), read(y), read(z);
push(v[x - A], {y - A, z});
push(v[y - A], {x - A, z});
}
read(k), read(t);
k -= A, t -= A;
if(k == t){
for(auto it:v[k]) if(it.to == k) cout << it.len;
}
else cout << dijkstra();
return 0;
}
这里空空如也
有帮助,赞一个