Extended Traffic
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/O
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.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1
Output
For 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
5
2
10 10
1
1 2
1
2
</big>
##Sample Output
<big>
Case 1:
3
4
Case 2:
?
</big>
##Hint
<big>
</big>
<br/>
##题意:
<big>
对图中的N点,给出q个询问,输出起点到询问点之间的最短路.
若不可达或最短路小于3,输出?.
</big>
<br/>
##题解:
<big>
本来看题目数据这么小,直接floyd就可以了,结果发现不太对.
如果图中存在负环,那么若询问负环中的点,都需要输出'?'.
所以在spfa中要增加一个环节,当判断到存在负环时,需要用dfs标记出当前负环上的所有点.
做题要仔细. 最短路要多考虑负环、重边、不联通.
</big>
<br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 5010
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int m,n,k;
int dis[maxn];
struct Edge
{
int v,cost;
Edge(int _v = 0, int _cost = 0)
{
v = _v;
cost = _cost;
}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
bool circle[maxn];
void dfs(int u) {
circle[u] = 1;
for(int i = 0;i < E[u].size();i++) {
if(!circle[E[u][i].v]) dfs(E[u][i].v);
}
}
queue<int> q;
bool inq[maxn];
int inq_cnt[maxn];
void spfa(int s) {
memset(inq, 0, sizeof(inq));
memset(inq_cnt, 0, sizeof(inq_cnt));
for(int i=1; i<=n; i++) dis[i] = inf; dis[s] = 0;
while(!q.empty()) q.pop();
q.push(s); inq_cnt[s]++;
while(!q.empty()) {
int p = q.front(); q.pop();
inq[p] = 0;
for(int i = 0;i < E[p].size();i++){
int v = E[p][i].v;
if(circle[v]) continue;
if(dis[v] > dis[p] + E[p][i].cost){
dis[v] = dis[p] + E[p][i].cost;
if(!inq[v]) {
q.push(v);
inq[v] = 1;
inq_cnt[v]++;
if(inq_cnt[v] > n) {
dfs(v);
//return 0;
}
}
}
}
}
//return 1;
}
int num[maxn];
int main(void)
{
//IN;
int t; cin >> t; int ca=1;
while(t--)
{
cin >> n;
memset(circle, 0, sizeof(circle));
//memset(first, -1, sizeof(first));
//edges = 0;
for(int i = 1;i <= n;i++)
E[i].clear();
for(int i=1; i<=n; i++) scanf("%d",&num[i]);
cin >> m;
for(int i=1; i<=m; i++) {
int u,v; scanf("%d %d",&u,&v);
int d = num[v] - num[u];
addedge(u,v,d*d*d);
}
spfa(1);
int q; cin >> q;
printf("Case %d:\n", ca++);
while(q--) {
int x; scanf("%d",&x);
if(circle[x] || dis[x]<3 || dis[x]==inf) printf("?\n");
else printf("%d\n", dis[x]);
}
}
return 0;
}