题目传送门

题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费

分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下,DFS实现.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int N = 2e2 + 5;
const int E = 1e4 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
int v, w, nex;
Edge() {}
Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[E];
int head[N];
int d[N];
int cnt[N];
int a[N];
bool vis[N], vis2[N];
int n, m, e; void init() {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[e] = Edge (v, w, head[u]);
head[u] = e++;
} void DFS(int u) {
vis2[u] = true;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v;
if (!vis2[v]) {
DFS (v);
}
}
} void SPFA(int s) {
memset (cnt, 0, sizeof (cnt));
memset (vis, false, sizeof (vis));
memset (vis2, false, sizeof (vis2));
memset (d, INF, sizeof (d));
d[s] = 0; cnt[s] = 0; vis[s] = true;
queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (vis2[v]) continue;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = true; que.push (v);
if (++cnt[v] > n) {
DFS (v);
}
}
}
}
}
} int cal(int i, int j) {
int ret = a[i] - a[j];
ret = ret * ret * ret;
return ret;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
init ();
scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
}
scanf ("%d", &m);
for (int u, v, i=1; i<=m; ++i) {
scanf ("%d%d", &u, &v);
add_edge (u, v, cal (v, u));
}
SPFA (1);
int q; scanf ("%d", &q);
printf ("Case %d:\n", ++cas);
while (q--) {
int x; scanf ("%d", &x);
if (d[x] == INF || d[x] < 3 || vis2[x]) puts ("?");
else printf ("%d\n", d[x]);
}
} return 0;
}

  

SPFA(负环) LightOJ 1074 Extended Traffic的更多相关文章

  1. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  2. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  3. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  4. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  5. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  6. (简单) LightOJ 1074 Extended Traffic,SPFA+负环。

    Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...

  7. LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)

    题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...

  8. LightOJ - 1074 Extended Traffic (SPFA+负环)

    题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...

  9. LightOJ - 1074 Extended Traffic(标记负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

随机推荐

  1. Sql如何自动定时备份数据库

    直接上图

  2. 电子现金、电子钱包、qPBOC、闪付、UPCash

    一.关于金融IC卡领域的规范 由Europay.Mastercard.Visa三大国际信用卡组织联合制定的金融集成电路(IC)卡金融支付标准,称为EMV规范,其目的是为金融IC卡.金融终端.支付系统以 ...

  3. 解决eclipseMavne的web项目debug时没有源码

  4. chaper3_exerise_Uva1568_Molar_Mass_分子量

    #include<iostream> #include<iomanip> #include<string> #include<cctype> using ...

  5. 自定义UIDatePikerView

    1.添加文件GoYearMonthDayPickerView.h .m .xib.NSDate+Helper.h .m.iCarousel.h .m 2.在Lable上显示日期 UILabel *ag ...

  6. NYOJ题目100 1的个数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsMAAAIqCAIAAABOpdBfAAAgAElEQVR4nO3drXLbzP834P9JhOdAgn ...

  7. hdu 2159 FATE

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 思路:二维完全背包,状态转移方程为: f[j][l]=max(f[j][l],f[j-b[i]] ...

  8. max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]

    在/etc/syscurity/limits.conf 加入以下两行: elastic hard nofile 65536 elastic soft nofile  65536 #备注:elastic ...

  9. 关于socket——SO_SNDBUF and SO_RECVBUF

    转自:http://blog.csdn.net/wf1982/article/details/38871521 参见 http://stackoverflow.com/questions/425741 ...

  10. linux 读写锁应用实例

    转自:http://blog.csdn.net/dsg333/article/details/22113489 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取 ...