941. Mr.L's Journey
题目描述
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 (3≤n≤100,0≤m≤n∗(n−1)/2,1≤s,t≤n,0≤k≤n) 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(1≤ki≤n) 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;
}