【CCF】行车路线 改编Dijkstra
2024-09-30 04:32:23
【AC】
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue> using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=5e2+;
const int maxm=1e5+;
int n,m;
struct edge{
int t;
int to;
int nxt;
ll w;
}e[*maxm];
struct node{
int id;
ll d;
ll fa;
node(int _id,ll _d,ll _fa):id(_id),d(_d),fa(_fa){}
bool operator < (const node& a) const{
if(d!=a.d) return d>a.d;
return fa>a.fa;
}
};
int head[maxn];
int tot;
ll dis[maxn];
ll fa[maxn];
bool vis[maxn];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int t,int u,int v,ll w){
e[tot].t=t;
e[tot].to=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
}
ll dijkstra(int s){
memset(dis,inf,sizeof(dis));
memset(fa,inf,sizeof(fa));
memset(vis,false,sizeof(vis));
dis[s]=,fa[s]=;
priority_queue<node> Q;
Q.push(node(s,dis[s],fa[s]));
while(!Q.empty()){
node q=Q.top();
Q.pop();
int u=q.id;
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].nxt){
int t=e[i].t;
int v=e[i].to;
ll w=e[i].w;
if(t==){
if(dis[u]+w<=dis[v]){
dis[v]=dis[u]+w;
fa[v]=;
Q.push(node(v,dis[v],fa[v]));
}
}else{
ll tmp=(fa[u]+w)*(fa[u]+w)+dis[u]-fa[u]*fa[u];
if(tmp<dis[v]){
dis[v]=tmp;
fa[v]=fa[u]+w;
Q.push(node(v,dis[v],fa[v]));
}else if(tmp==dis[v]){
if(fa[u]+w<fa[v]){
fa[v]=fa[u]+w;
Q.push(node(v,dis[v],fa[v]));
}
}
}
}
}
return dis[n];
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
int t,u,v;
ll w;
for(int i=;i<=m;i++){
scanf("%d%d%d%lld",&t,&u,&v,&w);
add(t,u,v,w);
add(t,v,u,w);
}
ll ans=dijkstra();
printf("%lld\n",ans);
}
return ;
}
最新文章
- IOS开发基础知识--碎片50
- Sokcet方式请求HTTP/HTTPS的封装类HttpHelper
- MAC 设置环境变量path的几种方法
- nosql数据库学习
- LInux配置jdk(mac和windows)
- EasyUI——常见用法总结
- hive查看建表语句
- 实例详解 DB2 排序监控和调优
- 内部类访问局部变量的时候,为什么变量必须加上final修饰
- hdoj 1231 最大连续子序列
- 自制单片机之十六……将文字或图形转成LCD上使用的C51字模数据
- JSP自定义标签——简单标签(2)
- 20140719中国互联网公司市值排名TOP20
- 保存和恢复 Android Fragment 的状态
- JS异步加载的三种方案
- 通过浏览器F12开发工具快速获取别的网站前端代码的方法
- spring初始化bean时执行某些方法完成特定的初始化操作
- Cartographer源码阅读(4):Node和MapBuilder对象2
- git 新建工程
- Jenkins + testNg + maven 项目持续集成