题目链接:LightOJ 1074
题意:
有n个路口,每个路口自身有一个值,m条边,每条边的权值是(终点路口值-起点路口值)^3,因此权值可为负。
有q次查询,每次查询输入一个终点路口(起点路口恒为1),问到达终点路口的最短路径是多少,如果最短路径少于3或者不可达,输出“?”,否则输出最短路径。
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 210;
const int maxm = 40010;
const int INF = 0x3f3f3f3f;
int cases, n, m, p, T, u, v, w, t;
int head[maxn], cnt[maxn], dis[maxn], value[maxn], vis[maxn];
int bad[maxn];
struct Edge {
int v, w, next;
}edge[maxm];
void AddEdge(int u, int v, int w, int k)
{
edge[k].v = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k;
}
void spfa(int s)
{
memset(cnt, 0, sizeof(cnt));
memset(bad, 0, sizeof(bad));
stack<int> q;
if (!q.empty()) q.pop();
for (int i = 1; i <= n; i++)
{
if (i == s) dis[i] = 0;
else dis[i] = INF;
}
memset(vis, 0, sizeof(vis));
vis[s] = 1;
q.push(s);
while (!q.empty())
{
int uu = q.top();
q.pop();
vis[uu] = 0;
for (int i = head[uu]; i != -1; i = edge[i].next)
{
int vv = edge[i].v;
int ww = edge[i].w;
if (dis[uu] < INF && dis[vv] > dis[uu] + ww)
{
dis[vv] = dis[uu] + ww;
if (!vis[vv])
{
if (++cnt[vv] > n) {
bad[vv] = 1;//采用这样标记负环的方式,虽然也AC了,但是总是感觉怪怪的。。。
//2168K 48MS
continue;
}
vis[vv] = 1;
q.push(vv);
}
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
scanf("%d", &T);
while (T--) {
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &value[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
int w = (value[v] - value[u])*(value[v] - value[u])*(value[v] - value[u]);
AddEdge(u, v, w, i);
}
spfa(1);
printf("Case %d:\n", ++cases);
scanf("%d", &p);
while (p--) {
scanf("%d", &t);
int ans = dis[t];
if (bad[t] || ans < 3 || ans == INF) printf("?\n");
else printf("%d\n", ans);
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 210;
const int maxm = 40010;
const int INF = 0x3f3f3f3f;
int cases, n, m, p, T, u, v, w, t;
int head[maxn], cnt[maxn], dis[maxn], value[maxn], vis[maxn];
bool cir[maxn];
struct Edge {
int v, w, next;
}edge[maxm];
void AddEdge(int u, int v, int w, int k)
{
edge[k].v = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k;
}
void dfs(int s)
{
cir[s] = true;
for (int i = head[s]; i != -1; i = edge[i].next)
if(!cir[edge[i].v]) dfs(edge[i].v);//不是cir[i]!
}
void spfa_(int s)
{
memset(cnt, 0, sizeof(cnt));
memset(cir, false, sizeof(cir));
stack<int> q;
if (!q.empty()) q.pop();
for (int i = 1; i <= n; i++)
{
if (i == s) dis[i] = 0;
else dis[i] = INF;
}
memset(vis, 0, sizeof(vis));
vis[s] = 1;
q.push(s);
while (!q.empty())
{
int uu = q.top();
q.pop();
vis[uu] = 0;
for (int i = head[uu]; i != -1; i = edge[i].next)
{
int vv = edge[i].v;
int ww = edge[i].w;
if (cir[vv]) continue;
if (dis[uu] < INF && dis[vv] > dis[uu] + ww)
{
dis[vv] = dis[uu] + ww;
if (!vis[vv])
{
if (++cnt[vv] > n) {
dfs(vv);//2164K 32MS
}
vis[vv] = 1;
q.push(vv);
}
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
scanf("%d", &T);
while (T--) {
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &value[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
int w= (value[v] - value[u])*(value[v] - value[u])*(value[v] - value[u]);
AddEdge(u, v, w, i);
}
spfa_(1);
printf("Case %d:\n", ++cases);
scanf("%d", &p);
while (p--) {
scanf("%d", &t);
int ans = dis[t];
if (cir[t] || ans < 3 || ans == INF) printf("?\n");
else printf("%d\n", ans);
}
}
return 0;
}