hdu2544 最短路(dijkstra/优先队列)

最短路

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 39750    Accepted Submission(s): 17334

Problem Description

在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input

输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。

输入保证至少存在1条商店到赛场的路线。

Output

对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间

Sample Input

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output

3
2

Source

UESTC 6th Programming Contest Online

前面有好几道最短路问题,方法都是一样(dijkstra)就不多说了。灵活运用就好了。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct node
{
	int pos,l;
	friend bool operator <(node x,node y)
	{
		return x.l>y.l;
	}
};
priority_queue<node>s;
int len[105][105],vis[105],n;
void dijkstra(int end)
{
	node temp,x;
	temp.l=0,temp.pos=1;
	s.push(temp);
	while(!s.empty())
	{
		temp=s.top(),x=temp;
		s.pop();
		int star=temp.pos;
		if(star==end)
		break;
		vis[star]=1;
		for(int i=2;i<=n;i++)
		{
			if(!vis[i]&&len[star][i]<2000)
			{
				temp.pos=i,temp.l+=len[star][i];
				s.push(temp);
			}
			temp=x;
		}
	}
	printf("%d\n",temp.l);
}
int main()
{
	int m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(m==0&&n==0)
		break;
		memset(len,100,sizeof(len));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<m;i++)
		{
			int a,b,x;
			scanf("%d %d %d",&a,&b,&x);
			if(x<len[a][b])
			len[a][b]=len[b][a]=x;
		}
		dijkstra(n);
		while(!s.empty())
		s.pop();
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 21:42:22

hdu2544 最短路(dijkstra/优先队列)的相关文章

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30007   Accepted: 10092 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

hdu2544 最短路 Dijkstra算法

最短路(Dijkstra算法模板题) Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 96778    Accepted Submission(s): 41849借鉴链接:https://blog.csdn.net/UncleJokerly/article/details/79703622 Problem Description 在每年的

最短路--dijkstra+优先队列优化模板

不写普通模板了,还是需要优先队列优化的昂 1 #include<stdio.h> //基本需要的头文件 2 #include<string.h> 3 #include<queue> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 typedef pair<int,int> pii; 8 const int INF=0x3f3f3f3f; 9 10 11

HDU2544 最短路 Dijkstra实现

题目 http://acm.hdu.edu.cn/showproblem.php?pid=2544 代码 #include <cstdio> #include <algorithm> #include <queue> #include <vector> using namespace std; const int INF = 1000000000; const int max_v = 110; const int max_e = 10010; struct

POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer John's field ha

UVA 658 It&#39;s not a Bug, it&#39;s a Feature! (单源最短路,dijkstra+优先队列,变形,经典)

题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了:要打多久才能无bug?(同1补丁可重复打) 分析: n<=20,那么用位来表示bug的话有220=100万多一点.不用建图了,图实在太大了,用位图又不好玩.那么直接用隐式图搜索(在任意点,只要满足转移条件,任何状态都能转). 但是有没有可能每个状态都要搜1次啊?那可能是100万*100万啊,这样出题

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

【poj 1724】 ROADS 最短路(dijkstra+优先队列)

ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N cities named with numbers 1 - N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that

poj1502 MPI Maelstrom,单源最短路的最长距离,dijkstra + 优先队列

点击打开链接 求顶点1到其他点的最短距离的最长距离.. 测试..dijkstra + 优先队列 #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <set> #include <map> #include <string> #include <sta