toj 2819 Travel

本文介绍了一个包含N个城市和M条道路的国家中寻找两座城市间最短距离的问题,并通过Dijkstra算法解决该问题。输入包括城市数量、道路数量及起点终点等信息,输出为最短距离。

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

2819.   Travel
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 1042    Accepted Runs: 458



There are N cities in this country, and some of them are connected by roads. Given two cities, can you find the shortest distance between them?

Input

The first line contains the number of test cases, then some test cases followed.

The first line of each test case contain two integers N and M (3 ≤ N ≤ 1000, M ≥ 0), indicating the number of cities and the number of roads. The next line contain two numbers S and T (ST), indicating the start point and the destination. Each line of the following M lines contain three numbers Ai, Bi and Ci (1 ≤ Ai,BiN, 1 ≤ Ci ≤ 1000), indicating that there is a road between city Ai and city Bi, whose length is Ci. Please note all the cities are numbered from 1 to N.

The roads are undirected, that is, if there is a road between A and B, you can travel from A to B and also from B to A.

Output

Output one line for each test case, indicating the shortest distance between S and T. If there is no path between them, output -1.

Sample Input

2
3 3
1 3
1 2 10
2 3 20
1 3 50
3 1
1 3
1 2 10

Sample Output

30
-1
Problem Setter: RoBa

Source: TJU Programming Contest 2007
Submit   List    Runs   Forum   Statistics

#include  < iostream >
#include 
< queue >
#define  MAX 1002
using   namespace  std;
typedef 
struct  node
{
    
int  di;
    
int  heap;
    node(){}
    node(
int  h, int  d)
    {
        heap
= h;
        di
= d;
    }
    friend 
bool   operator   < (node a,node b)
    {
        
return  a.di > b.di;
    }
}Point;
priority_queue
< Point > Q;
Point temp;
int  n,m,dis[MAX],t,s,e;
int  edges[MAX][MAX];
bool  mark[MAX];
void  Init()
{
    
int  i,j,a,b,ss;
    scanf(
" %d%d " , & n, & m);
    scanf(
" %d%d " , & s, & e);
    
for (i = 1 ;i <= n;i ++ )
        
for (j = 1 ;j <= n;j ++ )
            edges[i][j]
=- 1 ;
    
while (m -- )
    {
        scanf(
" %d%d%d " , & a, & b, & ss);
        
if (edges[a][b] ==- 1 || s < edges[a][b])
        {
            edges[a][b]
= ss;
            edges[b][a]
= ss;
        }
    }
}
void  Dijkstra( int  s)
{
    
int  i,j,k;
    
while ( ! Q.empty())
        Q.pop();
    
for (i = 1 ;i <= n;i ++ )
    {
        dis[i]
= edges[s][i];
        
if (dis[i] !=- 1 )
        {
            Q.push(node(i,dis[i]));
        }
        mark[i]
= false ;
    }
    mark[s]
= true ;
    
for (i = 1 ;i < n;i ++ )
    {
        k
=- 1 ;
        
while ( ! Q.empty())
        {
            temp
= Q.top();
            Q.pop();
            
if ( ! mark[temp.heap])
            {
                k
= temp.heap;
                
break ;
            }
        }
        
if (k ==- 1 )
        {
            printf(
" -1\n " );
            
return  ;
        }
        
for (j = 1 ;j <= n;j ++ )
        {
            
if (mark[j] || edges[k][j] ==- 1 )
                
continue ;
            
if (dis[k] + edges[k][j] < dis[j] || dis[j] ==- 1 )
            {
                dis[j]
= dis[k] + edges[k][j];
                Q.push(node(j,dis[j]));
            }
        }
    }
    printf(
" %d\n " ,dis[e]);
}
int  main()
{
    cin
>> t;
    
while (t -- )
    {
        Init();
        Dijkstra(s);
    }
    
return   0 ;
}

转载于:https://www.cnblogs.com/forever4444/archive/2009/05/14/1456968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值