Lightoj1019——Brush (V)(最短路)

本文介绍了一个经典的图论问题——寻找两个节点间的最短路径,并提供了一段使用 Dijkstra 算法实现的 C++ 代码示例。该算法应用于解决 Tanvir 如何找到从其住所到 Atiq 家的最短路线的问题。

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
Output for Sample Input
2

3 2
1 2 50
2 3 10

3 1
1 2 40
Case 1: 60
Case 2: Impossible

模板题了,就多了个判重。自己手敲了一遍,发现还是有点艰难。。。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <map>
#define INF 0x3f3f3f3f
#define MAXN 105
#define Mod 20007
using namespace std;
int n,m;
int vis[MAXN],mp[MAXN][MAXN],dis[MAXN];
void dijkstra(int v)
{
    for(int i=1;i<=n;++i)
            dis[i]=mp[v][i];
    dis[v]=0;
    vis[v]=1;
    for(int i=1;i<n;++i)
    {
        int minn=INF,p=-1,j;
        for(j=1;j<=n;++j)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                minn=dis[j];
                p=j;
            }
        }
        if(p==-1)
            break;
        vis[p]=1;
        for(int j=1;j<=n;++j)
            if(!vis[j]&&dis[j]>dis[p]+mp[p][j])
                dis[j]=dis[p]+mp[p][j];
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1; cas<=t; ++cas)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=n; ++j)
                mp[i][j]=INF;
        memset(vis,0,sizeof(vis));
        int u,v,w;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(w<mp[u][v])
            {
                mp[u][v]=w;
                mp[v][u]=w;
            }
        }
        dijkstra(1);
        if(dis[n]==INF)
            printf("Case %d: Impossible\n",cas);
        else
            printf("Case %d: %d\n",cas,dis[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值