HDU 3790 最短路径问题 双重权值

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3790

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
const int INF = 1<<30;
int d[maxn],vis[maxn],c[maxn];
struct node
{
	int d;//距离 
	int x;//点的编号 
	bool operator < (const node &a)const
	{
		if(this->d!=a.d)
		return this->d>a.d;
	}
};
struct edge
{
	int from,to;
	int w,h;
};
priority_queue<node> q;
vector<edge> e;
vector<int> g[maxn];
void addedge(int from,int to,int w,int h)
{
	edge t={from,to,w,h};
	e.push_back(t);
	edge t2={to,from,w,h};
	e.push_back(t2);
	int m=e.size();
	g[from].push_back(m-2);
	g[to].push_back(m-1);
}
void dijstra(int s,int n)
{
	while(!q.empty())q.pop();
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)d[i]=INF,c[i]=INF;
	d[s]=0,c[s]=0;
	node a={d[s],s};
	q.push(a);
	while(!q.empty())
	{
		node temp=q.top();q.pop();
		int u=temp.x;
		if(vis[u])continue;
		vis[u]=1;
		for(int i=0;i<g[u].size();i++)
		{
			int v=e[g[u][i]].to;
//			if(vis[v])continue;
			int w=e[g[u][i]].w;
			int H=e[g[u][i]].h;//最短距离最先考虑,相等时在按花费考虑 
			if(d[v]>d[u]+w)
			{
				d[v]=d[u]+w;
				c[v]=c[u]+H;
				q.push((node){d[v],v});
			}
			else if(d[v]==d[u]+w&&c[v]>c[u]+H)
			c[v]=c[u]+H;
		}
	}
}
int main()
{
	int n,m;
//	freopen("in.txt","r",stdin);
//	freopen("out1.txt","w",stdout);
	while(~scanf("%d%d",&n,&m))
	{
		if(m+n==0)break;
		e.clear();
		for(int i=1;i<=n;i++)g[i].clear();
		for(int i=0;i<m;i++)
		{
			int u,v,w,h;
			scanf("%d%d%d%d",&u,&v,&w,&h);
			addedge(u,v,w,h);
		}
		int s,t;
		scanf("%d%d",&s,&t);
		dijstra(s,n);
		printf("%d %d\n",d[t],c[t]);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值