题目链接:http://lightoj.com/volume_showproblem.php?problem=1074
Time Limit: 2 second(s) Memory Limit: 32 MB
Problem Description
Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
InputInput starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
OutputFor each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a '?'.
Sample Input
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
52
10 10
1
1 2
1
2
Output for Sample Input
Case 1:
3
4
Case 2:
?
Problem solving report:
Description: 对图中的n点,给出q个询问,输出起点到询问点之间的最短路.若不可达或最短路小于3,输出?.
Problem solving: 如果图中存在负环,那么若询问负环中的点,都需要输出'?',所以在spfa中要增加一个环节,当判断到存在负环时,需要用DFS标记出当前负环上的所有点.
Accepted Code:
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 205;
const int inf = 0x3f3f3f3f;
struct edge {
int u, v, w;
edge() {}
edge(int u, int v, int w) : u(u), v(v), w(w) {}
}e[MAXN * MAXN];
int cnt, n;
bool vis[MAXN], visp[MAXN];
int f[MAXN], inq[MAXN], dis[MAXN], w[MAXN];
void Add(int u, int v, int w) {
e[++cnt] = edge(f[u], v, w);
f[u] = cnt;
}
void init() {
cnt = 0;
memset(f, -1, sizeof(f));
memset(inq, 0, sizeof(inq));
memset(vis, 0, sizeof(vis));
memset(visp, 0, sizeof(visp));
memset(dis, 0x3f, sizeof(dis));
}
void DFS(int u) {
visp[u] = true;
for (int i = f[u]; ~i; i = e[i].u)
if (!visp[e[i].v])
DFS(e[i].v);
}
void Spfa(int s) {
queue <int> Q;
Q.push(s);
inq[s]++;
dis[s] = 0;
vis[s] = true;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
vis[u] = false;
for (int i = f[u]; ~i; i = e[i].u) {
int v = e[i].v;
if (visp[v])
continue;
if (dis[v] > dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
if (!vis[v]) {
Q.push(v);
vis[v] = true;
if (++inq[v] >= n)
DFS(v);
}
}
}
}
}
int main() {
int t, q, m, u, v, kase = 0;
scanf("%d", &t);
while (t--) {
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &w[i]);
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
Add(u, v, (w[v] - w[u]) * (w[v] - w[u]) * (w[v] - w[u]));
}
Spfa(1);
printf("Case %d:\n", ++kase);
scanf("%d", &q);
while (q--) {
scanf("%d", &v);
if (dis[v] < inf && dis[v] >= 3 && !visp[v])
printf("%d\n", dis[v]);
else printf("?\n");
}
}
return 0;
}