题目链接:https://cn.vjudge.net/problem/LightOJ-1074

题意

给一图

求最短路

若最短路<3或没有最短路,则输出'?'

思路

首先注意到可能存在负环,所以想到Bellman

这时注意检测出负环时,需要找到所有与其连通的节点(dfs处理)

然而这是一道怪题。。。

用long long存边权,WA5发

改成int存边权才过

怀疑我是哪里运算问题了

代码

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=220, maxm=maxn*maxn, INF=0x3f3f3f3f;
struct Edge{
int to, dis, next;
}edges[maxm+5];
int busy[maxn+5], head[maxn+5], esize;
int dist[maxn+5];
bool vis[maxn+5]; inline void addEdge(int from, int to, int dis){
edges[esize]=(Edge){to, dis*dis*dis, head[from]};
head[from]=esize++;
} void dfs(int x){
vis[x]=true;
for (int i=head[x]; i!=-1; i=edges[i].next){
Edge &e=edges[i]; if (vis[e.to]) continue;
dfs(e.to);
}
} void Bellman(int n){
bool inq[maxn+5]={false};
int cnt[maxn+5]={0};
queue<int> que; memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
dist[1]=0; inq[1]=true;
que.push(1); while (que.size()){
int from=que.front(); que.pop();
inq[from]=false; for (int i=head[from]; i!=-1; i=edges[i].next){
Edge &e=edges[i];
int &to=e.to, &dis=e.dis; if (vis[to]) continue;
if (dist[to]<=dist[from]+dis) continue;
dist[to]=dist[from]+dis; if (inq[to]) continue;
if (++cnt[to]>=n) dfs(to);
else que.push(to);
inq[to]=true;
}
}
} void init(void){
memset(head, -1, sizeof(head));
esize=0;
} int main(void){
int T, n, m, q, from, to; scanf("%d", &T);
for (int cnt=1; cnt<=T; cnt++){
init();
scanf("%d", &n);
for (int i=1; i<=n; i++) scanf("%d", &busy[i]);
scanf("%d", &m);
for (int i=0; i<m; i++){
scanf("%d%d", &from, &to);
addEdge(from, to, busy[to]-busy[from]);
} Bellman(n);
scanf("%d", &q);
printf("Case %d:\n", cnt);
while (q--){
scanf("%d", &from);
if (vis[from] || dist[from]<3 || dist[from]==INF) printf("?\n");
else printf("%d\n", dist[from]);
}
} return 0;
}
Time Memory Length Lang Submitted
48ms 1816kB 2033 C++ 2018-06-01 17:50:02

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+负环。

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

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

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

  7. SPFA(负环) LightOJ 1074 Extended Traffic

    题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...

  8. LightOJ 1074 Extended Traffic SPFA 消负环

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

  9. LightOJ 1074 - Extended Traffic 【SPFA】(经典)

    <题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...

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

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

随机推荐

  1. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  2. NTP同步底层实现

    RFC http://www.ietf.org/rfc/rfc5905.txt https://www.eecis.udel.edu/~mills/ntp/html/select.html https ...

  3. 硬核官宣:台积电官宣6nm及7nm加强版工艺!

    台积电正式宣布了6nm(N6)工艺,在已有7nm(N7)工艺的基础上大幅度增强,号称可提供极具竞争力的高性价比,而且能加速产品研发.量产.上市速度. 这几年,曾经执行业牛耳的Intel在新工艺方面进展 ...

  4. JavaScript变量提升(Hoisting)的小案例

    变量提升(Hoisting)的小案例 执行以下代码的结果是什么?为什么? 答案 这段代码的执行结果是undefined 和 2. 这个结果的原因是,变量和函数都被提升(hoisted) 到了函数体的顶 ...

  5. COGS——T 438. 烦人的幻灯片

    http://www.cogs.pro/cogs/problem/problem.php?pid=438 ★☆   输入文件:slides.in   输出文件:slides.out   简单对比时间限 ...

  6. java判断string数组中是否包含某个元素

  7. 【JavaEE WEB 开发】Tomcat 具体解释 Servlet 入门

    转载请注明出处 :  http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...

  8. Android DatePickerDialog样式不一致的问题

    三星和华为的平板上,DatePickerDialog的显示样式不一致.三星的仅仅显示月日年选择框,而华为的平板上另外还显示了日历表.代码同样. 可能是系统控件做了部分改动,后来你发现是能够设置的: D ...

  9. Unity学习笔记 之 发射小球碰撞物体的代码记录

    绑定在摄像机上的脚本 using UnityEngine; using System.Collections; public class abc : MonoBehaviour { //设置移动速度 ...

  10. Asp.net动态页面静态化之初始NVelocity模板引擎

    Asp.net动态页面静态化之初始NVelocity模板引擎 静态页面是网页的代码都在页面中,不须要运行asp,php,jsp,.net等程序生成client网页代码的网页,静态页面网址中一般不含&q ...