pat天梯赛练习集合 L3-007 天梯地图
加了一些花的最短路,点的个数为500不需要堆优化,改一下dij的判断条件就可以了。
上代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f; struct node
{
int id;
int cost;
int time;
}; int s,e;
int n,m;
vector<int> line0;
vector<int> line1;
vector<node> edge[maxn];
int d0[maxn];
int d1[maxn]; void build_map()
{
int v1,v2,ow,cost,time;
scanf("%d %d %d %d %d",&v1,&v2,&ow,&cost,&time);
node temp;
temp.cost=cost;
temp.time=time;
temp.id=v2;
edge[v1].push_back(temp);
if(ow==)
{
temp.id=v1;
edge[v2].push_back(temp);
}
} void dij0() // deug
{
int vis[maxn];
int pre[maxn];
int cost[maxn];
cost[s]=;
pre[s]=-;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) d0[i]=inf;
d0[s]=;
while()
{
int v=-;
for(int i=;i<=n;i++)
{
if(vis[i]== && ( v==- || d0[v] > d0[i])) v=i;
}
if(v==-) break;
vis[v]=;
int len=edge[v].size();
for(int i=;i<len;i++)
{
node temp=edge[v][i];
if(vis[temp.id] == )
{
if(d0[temp.id] > d0[v]+temp.time )
{
d0[temp.id]=d0[v]+temp.time; //
pre[temp.id]=v;
cost[temp.id]=cost[v]+temp.cost;
}
else if(d0[temp.id] == d0[v]+temp.time)
{
if(cost[temp.id] > cost[v]+temp.cost)
{
cost[temp.id]=cost[v]+temp.cost;
pre[temp.id]=v;
}
} }
}
}
// cout<<"1"<<endl;
int zz=e;
line0.push_back(e);
while(pre[zz]!=-) //
{
line0.push_back(pre[zz]);
zz=pre[zz];
// cout<<zz<<endl;
}
//cout<<"2"<<endl; reverse(line0.begin(),line0.end());
} void dij1() // deug
{
int vis[maxn];
int pre[maxn];
int cost[maxn];
cost[s]=;
pre[s]=-;
memset(vis,,sizeof(vis));
// vis[s]=1;
for(int i=;i<=n;i++) d1[i]=inf;
d1[s]=;
while()
{
int v=-;
for(int i=;i<=n;i++)
{
if(vis[i]== && (v==- || d1[v] > d1[i])) v=i;
}
if(v==-) break;
vis[v]=;
// line1.push_back(v);
int len=edge[v].size();
for(int i=;i<len;i++)
{
node temp=edge[v][i];
if(vis[temp.id]==)
{
if(d1[temp.id] > d1[v]+temp.cost)
{
d1[temp.id]=d1[v]+temp.cost; //
pre[temp.id]=v;
cost[temp.id]=cost[v]+;
}
else if(d1[temp.id] == d1[v]+temp.cost)
{
if(cost[temp.id] > cost[v]+)
{
cost[temp.id]=cost[v]+;
pre[temp.id]=v;
}
}
} }
} int zz=e; // route
line1.push_back(e);
while(pre[zz]!=-)
{
line1.push_back(pre[zz]);
zz=pre[zz];
} reverse(line1.begin(),line1.end());
}
int check()
{
int len1=line1.size();
int len0=line0.size();
if(len1 != len0) return ;
for(int i=;i<len1;i++)
{
if(line0[i] != line1[i]) return ;
}
return -;
} int main()
{
cin>>n>>m;
while(m--) build_map();
cin>>s>>e;
dij0();// 0 refers to the shortest time
dij1();// 1 refers to the shortset cost
if(check() == ) // diff
{
printf("Time = %d: ",d0[e]);
cout<<line0[];
for(int i=;i<line0.size();i++) cout<<" => "<<line0[i];
cout<<endl; printf("Distance = %d: ",d1[e]);
cout<<line1[];
for(int i=;i<line1.size();i++) cout<<" => "<<line1[i];
cout<<endl;
}
else
{
printf("Time = %d; ",d0[e]);
printf("Distance = %d: ",d1[e]);
cout<<line1[];
for(int i=;i<line1.size();i++) cout<<" => "<<line1[i];
cout<<endl;
}
return ;
}
pat天梯赛练习集合 L3-007 天梯地图的更多相关文章
- PAT 天梯赛 L2-005 集合相似度
set的应用 题目链接 题解 有点像集合的交并操作,直接利用set进行处理,因为set有去重的功能,而且set是利用红黑树实现的,查找速度快O(logN). 代码如下: #include<cst ...
- PAT天梯赛L2-005 集合相似度
题目链接:点击打开链接 给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你的任务就是计算任意一对给定集合的 ...
- PAT 天梯赛 L2-005. 集合相似度 【SET】
题目链接 https://www.patest.cn/contests/gplt/L2-005 思路 因为集合中的元素 是不重复的 所以用SET 来保存 集合 然后最后 查找一下 两个集合中共有元素 ...
- 天梯赛 L2-005 集合相似度 (set容器)
给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你的任务就是计算任意一对给定集合的相似度. 输入格式: 输 ...
- 天梯赛 - L2-005 集合相似度
题目链接:https://www.patest.cn/contests/gplt/L2-005 这个题理解是个大问题啊,“给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合 ...
- PAT天梯赛 L1-049 天梯赛座位分配
题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...
- PAT L1 049 天梯赛座位分配
天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] 支队伍,每队 10 位 ...
- PAT天梯赛L3-007 天梯地图
题目链接:点击打开链接 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至 ...
- pat 团体天梯赛 L3-007. 天梯地图
L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...
随机推荐
- vue报错:There are multiple modules with names that only differ in casing.
今天写项目时,遇到报错信息如下: 经过多次排除及参考网上文章,最后找到问题所在 排查原因:1 .在引用组件时,路径大小写不对也会造成此报错,看例子:错误写法: 正确写法: 2.在组件使用vuex时,引 ...
- 006 GET API
1.说明 The get API allows to get a JSON document from the index based on its id. GET通过基于id的索引获取JSON文档. ...
- git merge 结果是 git merge Already up-to-date. 该怎么解决?
git将主干合并到当前分支时,出现如下结果: 原因在于:执行git merge前,主干的代码没有更新 正确的操作步骤如下: 1 .切换到主干 $ git checkout master 2. 更新主干 ...
- Python3基础 二、八、十、十六进制转换
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- 如何切换svn的登陆账号?
如何切换svn的登陆账号? 听语音 原创 | 浏览:68661 | 更新:2017-10-06 09:09 1 2 3 4 5 6 分步阅读 对于程序员来说,svn使用的比较广泛,平时用来更新或者是提 ...
- k8s记录-ntpd时间同步配置(五)
1)服务端配置 在192.168.0.1 root用户下操作 yum install -y ntp ntpdate 修改etc/ntp.conf 注释所有的server和restrict 加入: se ...
- linux信号量例子
semaphore.h 提供的是 POSIX 标准定义的 semaphore 接口,而 sys/sem.h 里 提供的是符合 System V 标准的 semaphore接口 (semget, sem ...
- [ kvm ] 学习笔记 7:KVM 虚拟机创建的几种方式
通过对 qemu-kvm.libvirt 的学习,总结三种创建虚拟机的方式: (1)通过 qemu-kvm 创建 (2)通过 virt-install 创建 (3)通过 virt-manager 创建 ...
- elasticsearch5.0.1集群索引分片丢失的处理
elasticdump命令安装 yum install npm npm install elasticdump -g 命令安装完毕,可以测试. 可能会报出nodejs的版本之类的错误,你需要升级一下版 ...
- LODOP打印table超宽用省略号带'-'的内容换行问题
前面的博文有div超宽隐藏(LODOP打印超过后隐藏内容样式),还有有table设置超宽隐藏(),此外,还有超宽后用省略号表示的css样式,此文是针对这个样式的.该样式正常情况下没问题,但是遇到-短线 ...