最短路径问题 (堆优化版 + 双权值 的 dijkstra )

news/2024/7/15 18:54:08 标签: 算法, 图论

最短路径问题

给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。

Input

输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)

Output

输出 一行有两个数, 最短距离及其花费。

Sample Input

3 2
1 2 5 6
2 3 4 5
1 3
0 0

Sample Output

9 11

双权值问题,一个长度一个花费
按题目要求:要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的
读入数据时要注意一下,同时在判断长度和花费的时候与读入数据一致,都需要额外注意一下,刚开始的时候没有注意,然后就一直 TLE ,TLE 了几十发才注意到…孩子哭了在这里插入图片描述
AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII; 
const int N=1010;
int n,m;
int h[N],e[N],ne[N],w[N],idx;
int dist[N];
int cost[N];
bool vis[N];
int st,ed;
struct Node{
	int l;
	int c;
}node[N][N];

void add(int a,int b,int c)
{
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}

int dijkstra()
{
	memset(dist,0x3f,sizeof dist);
	memset(cost,0x3f,sizeof cost);
	memset(vis,0,sizeof vis);
	dist[st]=0;
	cost[st]=0;
	priority_queue<PII,vector<PII>,greater<PII>> heap;
	heap.push({0,st});
	// first 距离  second 点 
	while(heap.size())
	{
		int t=heap.top().second;
		heap.pop();
		if(vis[t]) continue;
		vis[t]=true;
		for(int i=1;i<=n;i++)
		{
			if(dist[i]>dist[t]+node[t][i].l)
			{
				dist[i]=dist[t]+node[t][i].l;
				cost[i]=cost[t]+node[t][i].c; 
				heap.push({dist[i],i});
			}
			else if(dist[i]==dist[t]+node[t][i].l&&cost[i]>cost[t]+node[t][i].c)
			{
				cost[i]=cost[t]+node[t][i].c;
				heap.push({dist[i],i});
			}
		}
	}
	cout<<dist[ed]<<" "<<cost[ed]<<endl;
}

int main()
{
	while(cin>>n>>m&&n&&m)
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				node[i][j].c=node[i][j].l=0x3f3f3f3f;
		for(int i=1;i<=m;i++)
		{
			int a,b,c,d;
			scanf("%d%d%d%d",&a,&b,&c,&d);
            if(node[a][b].l>c){
                node[a][b].l=node[b][a].l=c;
                node[a][b].c=node[b][a].c=d;
            }else if(node[a][b].l==c){
                node[a][b].c=node[b][a].c=min(d,node[a][b].c);
            }
		}
		cin>>st>>ed;
		dijkstra();
	}
	return 0;
}



http://www.niftyadmin.cn/n/1681414.html

相关文章

2014计算机应用基础试题及答案,2014年计算机应用基础试题及答案.doc

PAGEPAGE 24计算机应用基础2资料一、单选题1、第一台电子计算机是1946年在美国研制成功的&#xff0c;该机的英文缩写名是______。答案&#xff1a; AA&#xff1a;ENIAC B&#xff1a;EDVAC C&#xff1a;EDSAC D&#xff1a;MARK2、关于计算机的分类方法有多种&#xff0c;下…

使用VMware vCenter Converter迁移到虚拟机(p2v)

一、概述当VMware vSphere基础架构搭建好后&#xff0c;如何把现有跑在物理机上的应用迁移到虚拟机上呢&#xff1f;这就需要请出VMware vCenter Converter这个工具&#xff0c;此工具可以实现P2V&#xff08;物理机在线或离线迁移到虚拟机&#xff09;、V2V&#xff08;VMware…

Choose the best route ( dijkstra 路径翻转 )

Choose the best route One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki…

《大型网站技术架构》学习笔记——大型网站核心架构要素

1、性能网站速度快不快。优化网站性能手段包括&#xff1a;1&#xff09;优化浏览器端&#xff0c;浏览器缓存&#xff0c;页面压缩&#xff0c;合理页面布局&#xff0c;减少COOKIE传递2&#xff09;CDN&#xff0c;反向代理3&#xff09;缓存4&#xff09;异步操作&#xff0…

paip.点击每个网页链接都提示下载的解决。

paip.点击每个网页链接都提示下载的解决。作者Attilax 艾龙&#xff0c; EMAIL:1466519819qq.com 来源&#xff1a;attilax的专栏地址&#xff1a;http://blog.csdn.net/attilax点击每个网页链接都提示下载。。。应该是go才对。。原因:可能win键卡住键盘卡键的排查 单独按键。…

MFC线程中使用AfxMessageBox事项(别在线程中使用AfxMessageBox)

2019独角兽企业重金招聘Python工程师标准>>> 不要在线程中使用AfxMessageBox,原因我觉得有两个:1.阻塞 2.关闭AfxMessageBox后主线程就直接结束了. 例如: 在一个线程函数中这样写 <!-- lang: cpp --> UINT WINAPI CXXX::ChildThread(LPVOID pData) {//.......…

搜索与图论 ---- 拓扑排序

拓扑序列 拓扑序列是有向无环图按拓扑排序生产的序列&#xff0c;&#xff08;有向无环图一定有拓扑序&#xff0c;反之一定没有拓扑序&#xff09;&#xff0c;对于有向无环图&#xff0c;一定存在一个入度为 0 的点&#xff0c;该点是拓扑序的第一个点&#xff0c;将该点拿出…

Centos7.3安装,并设置网络和防火墙

下载centos7.3安装ISO 最小化安装&#xff0c;随后打通网络&#xff0c;完成网络设置、安装VIM&#xff0c;关闭firewalld防火墙&#xff0c;打开iptables防火墙重启&#xff0c; vim /etc/sysconfig/network-scripts/ifcfg-ens32 修改 onbootyes …