Lightoj1019 Brush (V) Dijkstra最短路

在一次充满挑战的城市探险中,Tanvir 和 Atiq 的故事揭示了如何利用 Dijkstra 算法解决实际问题,即在城市迷宫中找到最短路径。通过输入城市布局和道路距离,读者可以跟随 Tanvir 的脚步,了解如何计算从起点到终点的最短路径距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Brush (V)

 

Tanvir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found that there is no brush in him room. So, he called Atiq to get a brush. But as usual Atiq refused to come. So, Tanvir decided to go to Atiq's house.

The city they live in is divided by some junctions. The junctions are connected by two way roads. They live in different junctions. And they can go to one junction to other by using the roads only.

Now you are given the map of the city and the distances of the roads. You have to find the minimum distance Tanvir has to travel to reach Atiq's house.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. The next line contains two integers N (2 ≤ N ≤ 100) and M (0 ≤ M ≤ 1000), means that there are N junctions and M two way roads. Each of the next M lines will contain three integers u v w (1 ≤ u, v ≤ N, w ≤ 1000), it means that there is a road between junction u and v and the distance is w. You can assume that Tanvir lives in the 1st junction and Atiq lives in the Nth junction. There can be multiple roads between same pair of junctions.

Output

For each case print the case number and the minimum distance Tanvir has to travel to reach Atiq's house. If it's impossible, then print 'Impossible'.

Sample Input

2

 

3 2

1 2 50

2 3 10

 

3 1

1 2 40

Sample Output

Case 1: 60

Case 2: Impossible

思路:

Dijkstra最短路板子题

代码:

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 110;
const int MAXN = (int)1e3 + 10;
typedef pair<int,int> pir;
int n,m,kase = 1;

struct Edge
{
	int to,next,val;
	Edge(){}
	Edge(int _to,int _next,int _val)
	{
		to = _to,next = _next,val = _val;
	}
}edge[MAXN << 1];

int head[maxn],top;
void init(int n)
{
	memset(head,-1,sizeof(int) * (n + 1));
	top = 0;
}
void add(int u,int v,int val)
{
	edge[top] = Edge(v,head[u],val);
	head[u] = top ++;
}
void getmap(int m)
{
	int u,v,val;
	while (m --)
	{
		scanf("%d %d %d",&u,&v,&val);
		add(u,v,val);
		add(v,u,val);
	}
}

int dis[maxn];
void djk(int st,int end)
{
	memset(dis,0x3f,sizeof(int) * (n + 1));
	priority_queue <pir,vector<pir>,greater<pir> > que;
	dis[st] = 0,que.push(make_pair(0,st));
	while (!que.empty())
	{
		pir p = que.top();
		que.pop();
		int v = p.second;
		if (dis[v] < p.first)
			continue;
		for (int i = head[v]; ~i ;i = edge[i].next)
		{
			Edge e = edge[i];
			if (dis[e.to] > dis[v] + e.val)
			{
				dis[e.to] = dis[v] + e.val;
				que.push(make_pair(dis[e.to],e.to));
			}	
		}	
	}
	if (dis[end] == inf)
		printf("Impossible\n");
	else
		printf("%d\n",dis[end]);
}
int main()
{
	int t;
	scanf("%d",&t);
	while (t --)
	{
		scanf("%d %d",&n,&m);
		init(n);
		getmap(m);
		printf("Case %d: ",kase ++);
		djk(1,n);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值