当前位置:网站首页>PAT 1163 Dijkstra Sequence(30)
PAT 1163 Dijkstra Sequence(30)
2022-08-01 12:51:00 【此杭非彼航】
题目
代码
参考代码
// 相等的时候有多种取法, 逐一判断
#include<bits/stdc++.h>
using namespace std;
int inf = 0x7f7f7f7f;
int edge[1100][1100];
int dist[1100];
bool visit[1100];
int main() {
fill(edge[0], edge[0] + 1100 * 1100, inf);
int nv, ne;
cin >> nv >> ne;
for(int i = 0; i < ne; i++) {
int a, b, weight;
cin >> a >> b >> weight;
edge[a][b] = edge[b][a] = weight;
}
//cout << edge[3][4] << endl;
// 开始判断
int k;
cin >> k;
for(int i = 0; i < k; i++) {
// 记录一下待判断的seq
vector<int> v(nv);
// 存一下
for(int j = 0; j < nv; j++)
cin >> v[j];
//cout << v[0] << endl;
// 开始判断
// 初始化dist, visit
fill(dist, dist + 1100, inf);
fill(visit, visit + 1100, false);
// 起始点
dist[v[0]] = 0;
// 依次检查
bool flag = true;
for(int j = 0; j < nv; j++) {
// 寻找最小的dist, 判断是否符合
int minn = inf;
vector<int> record;
for(int k = 1; k <= nv; k++) {
if(visit[k] == false) {
if(dist[k] == minn) record.push_back(k);
else if(dist[k] < minn) {
minn = dist[k];
record.clear();
record.push_back(k);
}
}
}
int checkpoint = -1;
/* for(int k = 0; k < record.size(); k++) cout << record[k] << " "; cout << endl; */
for(int k = 0; k < record.size(); k++) {
if(record[k] == v[j]) {
checkpoint = k;
break;
}
}
// 看看能不能找到
if(checkpoint == -1) {
flag = false;
break;
}
// 如果可以找到
int u = v[j];
visit[u] = true;
// 后面更新dist
for(int v = 1; v <= nv; v++) {
if(visit[v] == false) {
if(dist[v] > dist[u] + edge[u][v]) dist[v] = dist[u] + edge[u][v];
}
}
}
if(flag == false)
cout << "No" << endl;
else cout << "Yes" << endl;
}
return 0;
}
复现代码
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int vis[1005],dis[1005],edge[1005][1005];
int main()
{
memset(edge,inf,sizeof(edge));
int nv,ne;
cin>>nv>>ne;
while(ne--){
int x,y,z;
cin>>x>>y>>z;
edge[x][y]=edge[y][x]=z;
}
int cnt;
cin>>cnt;
while(cnt--){
vector<int> v(nv);
for(int i=0;i<nv;i++){
cin>>v[i];
}
fill(dis,dis+1005,inf);
fill(vis,vis+1005,0);
dis[v[0]]=0;//注意顺序
int flag=1;
for(int t=0;t<nv;t++){
int minn=inf;
vector<int> point;
for(int i=0;i<nv;i++){
if(!vis[v[i]]){
if(dis[v[i]]<minn){
minn=dis[v[i]];
point.clear();
point.push_back(v[i]);
}
else if(dis[v[i]]==minn){
point.push_back(v[i]);
}
}
}
int check=-1;
for(int i=0;i<point.size();i++){
if(point[i]==v[t]){
check=point[i];
}
}
if(check==-1){
flag=0;
break;
}
int u=v[t];
vis[u]=1;
for(int v=1;v<=nv;v++){
if(!vis[v]&&dis[u]+edge[u][v]<dis[v]){
dis[v]=dis[u]+edge[u][v];
}
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
参考文章
边栏推荐
- 高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
- 阿里云官方 Redis 开发规范
- 计算器:中缀表达式转后缀表达式
- [Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
- Multi-threaded cases - blocking queue
- How to Integrate Your Service Registry with Istio?
- MySQL调优
- Js手写函数之new的模拟实现
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- This article will take you to thoroughly clarify the working mechanism of certificates in Isito
猜你喜欢
通讯录(静态版)(C语言)(VS)
那些利用假期学习的职场人,后来都怎么样了?
JMP Pro 16.0软件安装包下载及安装教程
JMP Pro 16.0 software installation package download and installation tutorial
leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
Beyond Compare 4 试用期到期
安装apex报错
快速幂---学习笔记
CloudCompare&PCL ICP配准(点到面)
windows IDEA + PHP+xdebug 断点调试
随机推荐
动态库、静态库浅析
How to Integrate Your Service Registry with Istio?
windows IDEA + PHP+xdebug 断点调试
芝加哥丰田技术学院 | Leveraging Natural Supervision for Language Representation Learning and Generation(利用自然监督进行语言表示学习和生成)
Meshlab & Open3D SOR filtering
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
The obstacles to put Istio into production and how we solve them
六石编程学:问题要面对,办法要技巧,做不好的功能要想办法
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
大中型网站列表页翻页过多怎么优化?
达梦更换正式授权dm.key
SQL function SQRT
How to integrate 3rd party service center registration into Istio?
Js手写函数之new的模拟实现
蔚来又一新品牌披露:产品价格低于20万
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
这项工作事关中小学生生命安全!五部门作出联合部署
Programmer's Romantic Tanabata
fh511小风扇主控芯片 便携式小风扇专用8脚IC 三档小风扇升压芯片sop8
Istio投入生产的障碍以及如何解决这些问题