传送门:Light oj - 1074
题目描述
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 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.
输出描述
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 ‘?’.
思路分析
1为源点,(u,v)的距离为(m[v] -m[u])3的,如果从源点到该点的距离小于3则输出?,否则输出距离。如果存在负环,那负环上的点到源点的距离肯定小于3
样例输入
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
样例输出
Case 1:
3
4
Case 2:
?
AC代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int INF = 0x3f3f3f3f;
const int N=222;
inline int read () {
register int s = 0, w = 1;
register char ch = getchar ();
while (! isdigit (ch)) {if (ch == '-') w = -1; ch = getchar ();}
while (isdigit (ch)) {s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar ();}
return s * w;
}
int a[N],e[N][N],vis[N],dis[N],isring[N],num[N],cur[N];
int n;
void dfs(int x) { //找负环上的点
isring[x]=1;
for(int i=1; i<=n; i++) {
if(e[x][i]!=INF && !isring[i])
dfs(i);
}
}
void spfa() {
fill(dis,dis+N,INF);
queue<int>q;
vis[1]=1;
dis[1]=0;
q.push(1);
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=0;
for(int i=1; i<=n; i++) {
if(isring[i])
continue; //如果已经是负环上的点,则不再入队列
if(dis[i]>dis[u]+e[u][i]&&e[u][i]!=INF) {
dis[i]=dis[u]+e[u][i];
if(!vis[i]) {
vis[i]=1;
q.push(i);
cur[i]=cur[u]+1;
if(cur[i]>=n) //如果某条路的边有n条,那么一定存在负环
dfs(i);
}
}
}
}
}
int main() {
int t,cnt=0;
t=read();
while(t--) {
memset(cur,0,sizeof(num));
memset(isring,0,sizeof(isring));
memset(vis,0,sizeof(vis));
n=read();
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++)
e[i][j]=INF;
}
for(int i=1; i<=n; i++)
a[i]=read();
int m;
m=read();
for(int i=0; i<m; i++) {
int x,y;
x=read();
y=read();
e[x][y]=pow((a[y]-a[x]),3);
}
spfa();
int tt;
tt=read();
printf("Case %d:\n",++cnt);
while(tt--) {
int x;
x=read();
if(dis[x]<3||dis[x]==INF||isring[x])
printf("?\n");
else
printf("%d\n",dis[x]);
}
}
return 0;
}