HDU 2992 Hotel booking(spfa+floyd)

spfa求到可到达点的最短路径,如果小于600说明可以到达,然后标记一下。在floyd求出有多少个点。然后减一。

Hotel booking

Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 357    Accepted Submission(s): 128


Problem Description
A transport company often needs to deliver goods from one city to another city. The transport company has made a special deal with a hotel chain which allows its drivers to stay in the hotels of this chain for free. Drivers are only allowed to drive up to 10 hours a day. The transport company wants to find a route from the starting city to the destination city such that a driver can always spend the night in one of the hotels of the hotel chain, and that he needs to drive at most 10 hours from one hotel to the next hotel (or the destination). Of course, the number of days needed to deliver the goods should also be minimized. 

 

Input
The input file contains several test cases. Each test case starts with a line containing an integer n, (2 ≤ n ≤ 10000), the number of cities to be considered when planning the route. For simplicity, cities are numbered from 1 to n, where 1 is the starting city, and n is the destination city. The next line contains an integer h followed by the numbers c 1, c 2, ..., c h indicating the numbers of the cities where hotels of the hotel chain are located. You may assume that 0 ≤ h ≤ min(n, 100). The third line of each test case contains an integer m (1 ≤ m ≤ 10 5), the number of roads to be considered for planning the route. The following m lines describe the roads. Each road is described by a line containing 3 integers a, b, t (1 ≤ a, b ≤ n and 1 ≤ t ≤ 600), where a, b are the two cities connected by the road, and t is the time in minutes needed by the driver to drive from one end of the road to the other. Input is terminated by n = 0. 

 

Output
For each test case, print one line containing the minimum number of hotels the transport company has to book for a delivery from city 1 to city n. If it is impossible to find a route such that the driver has to drive at most 10 hours per day, print -1 instead.
 

Sample Input
  
  
6 3 2 5 3 8 1 2 400 3 2 80 3 4 301 4 5 290 5 6 139 1 3 375 2 5 462 4 6 300 3 0 2 1 2 371 2 3 230 0
 

Sample Output
  
  
2 -1
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
//#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898

using namespace std;

const int maxn = 10010;


struct node
{
    int u, v, w;
    node (int vv, int ww)
    {
        v = vv;
        w = ww;
    }
};
int g[110][110];
int dis[maxn];
int st[maxn];
int inque[maxn];
int n, num;

vector<node>f[maxn];
map<int , int>mp;
queue<int>que;
void spfa(int start)
{
    for(int i = 0; i <= n; i++)
    {
        inque[i] = 0;
        dis[i] = INF;
    }
    while(!que.empty())que.pop();
    que.push(start);
    dis[start] = 0;
    inque[start] = 1;
    while(!que.empty())
    {
        int s = que.front();
        que.pop();
        inque[s] = 0;
        for(int i = 0; i < f[s].size(); i++)
        {
            int v = f[s][i].v;
            int w = f[s][i].w;
            if(dis[v] > dis[s]+w)
            {
                dis[v] = dis[s]+w;
                if(!inque[v])
                {
                    inque[v] = 1;
                    que.push(v);
                }
            }
        }
    }
    for(int i = 0; i <= num; i++)
    {
        if(dis[st[i]] <= 600)
        {
            g[mp[start]][mp[st[i]]] = 1;
        }
    }
}

void floyd()
{
    for(int k = 0; k <= num; k++)
        for(int i = 0; i <= num; i++)
            for(int j = 0; j <= num; j++)
                g[i][j] = min(g[i][j], g[i][k]+g[k][j]);
}

int main()
{
    while(~scanf("%d",&n))
    {
        if(!n)
            break;
        scanf("%d",&num);
        mp.clear();
        for(int i = 0; i <= n; i++)
            f[i].clear();
        for(int i = 0; i <= num+2; i++)
        {
            for(int j = 0; j <= num+2; j++)
            {
                g[i][j] = INF;
                if(i == j)
                    g[i][j] = 0;
            }
        }
        for(int i = 1; i <= num; i++)
        {
            scanf("%d",&st[i]);
            mp[st[i]] = i;
        }
        st[0] = 1;
        mp[1] = 0;
        st[num+1] = n;
        mp[n] = num+1;
        num++;
        int m;
        scanf("%d",&m);
        int u, v, w;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d %d %d",&u, &v, &w);
            f[u].push_back(node(v,w));
            f[v].push_back(node(u,w));
        }
        for(int i = 0; i < num; i++)
            spfa(st[i]);
        floyd();
        if(g[0][num] == INF)
            cout<<"-1"<<endl;
        else
            cout<<g[0][num]-1<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值