buptoj 941:Mr.L's Journey

时间限制 1000 ms  内存限制 65536 KB

题目描述

During the summer vocation,Mr.L wants to travel to Berland.There are n cities and m bidirectional roads in that lovely country, Mr.L starts his trip at city s.For some reasons,he wants to arrive at city t as soon as possible.In his travel list, there are also k cities that he thinks are beautiful.How can he get to his destination in the fastest way with  the most  beautiful cities traveled in his path?You can assume that the length of each road is 1.

输入格式

The first line contains integers n, m, s, t, k  (3n100,0mn(n1)/2,1s,tn,0kn)  separated by spaces.Then the next m lines contain m pairs of city (xi,yi)   means the city xi and city yi connect by a road .The last line contains k distinct integers  ki(1kin)   means the city ki is a beautiful city  separated by spaces.

输出格式

Only a line contains two integers separated by spaces, the first one is the shortest length and the second is the maximum number of beautiful cities he can pass in the fastest way.It guarantees that there is always a path from s to t.

输入样例

3 2 1 3 2
1 2
2 3
1 3

输出样例

2 2
一开始题意理解错了,题意是走最短路的情况下,经过的最多美丽城市数量。而不是为了走最多美丽城市数量去走路,后者想的话感觉就会有一些麻烦 。

spfa那个队列变成优先队列,优先找美丽城市,再回溯查数量。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
 
#define INF 0x33ffffff
 
#define eps 1e-6
const ll mod = 1000000007;
const int maxn = 505;
const double PI = acos(-1.0);
 
int n, m, s, t, k;
struct ed
{
    int to;
    int next;
}edge[maxn*maxn];
priority_queue<int>qu;
 
int edgen, res;
int stac[maxn], head[maxn], d[maxn], vis[maxn], pre[maxn];
 
void addedge(int u, int v)
{
    edgen++;
    edge[edgen].to = v;
    edge[edgen].next = head[u];
    head[u] = edgen;
}
int u[maxn], v[maxn], f[maxn], visit[maxn], con[maxn][maxn];
 
void init()
{
    edgen = 0;
    res = 0;
    memset(edge, -1, sizeof(edge));
    memset(head, -1, sizeof(head));
    memset(f, 0, sizeof(f));
    memset(visit, 0, sizeof(visit));
    memset(con, 0, sizeof(con));
    while (!qu.empty())qu.pop();
}
 
void spfa()
{
    int i, j;
    for (i = 0; i <= 200; i++)
    {
        d[i] = INF;
        vis[i] = 0;
        pre[i] = i;
    }
    vis[s] = 1;
    d[s] = 0;
    qu.push(s);
 
    while (!qu.empty())
    {
        int vv = qu.top();
        qu.pop();
 
        for (i = head[vv]; i != -1; i = edge[i].next)
        {
            int vt = edge[i].to;
            if (d[vt] > d[vv] + 1)
            {
                d[vt] = d[vv] + 1;
                pre[vt] = vv;
                if (!vis[vt])
                {
                    vis[vt] = 1;
                    qu.push(vt);
                }
            }
        }
        vis[vv] = 0;
    }
    i = t;
    while (pre[i] != i)
    {
        if (i > 100)
        {
            res++;
        }
        i = pre[i];
    }
    if (s > 100)
        res++;
}
 
void solve()
{
    int i, j;
    for (i = 1; i <= m; i++)
    {
        scanf("%d%d", &u[i], &v[i]);
        if (con[u[i]][v[i]] == 1)
        {
            u[i] = -1;
            v[i] = -1;
        }
        else
        {
            con[u[i]][v[i]] = con[v[i]][u[i]] = 1;
        }
    }
    for (i = 1; i <= k; i++)
    {
        scanf("%d", &j);
        f[j] = 1;
    }
    if (f[s])s += 100;
    if (f[t])t += 100;
    for (i = 1; i <= m; i++)
    {
        if (u[i] == -1)continue;
        if (f[u[i]])u[i] += 100;
        if (f[v[i]])v[i] += 100;
        addedge(u[i], v[i]);
        addedge(v[i], u[i]);
    }
    spfa();
    printf("%d %d\n", d[t], res);
}
 
int main()
{

    while (scanf("%d%d%d%d%d", &n, &m, &s, &t, &k) != EOF)
    {
        init();
        solve();
    }
    //system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值