(简单) LightOJ 1074 Extended Traffic,SPFA+负环。
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.
题目就是求最短路问题,但是可能存在负环,存在的话就需要标记所有负环上的点,输出?。
(但是坑的是判断到负环直接退出居然也AC了。。。。。。)
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=MaxN*MaxN;
const int INF=10e8; struct Edge
{
int to,next,cost;
}; Edge E[MaxM];
int head[MaxN],Ecou;
bool vis[MaxN];
int couNode[MaxN];
bool cir[MaxN]; void init(int N)
{
Ecou=; for(int i=;i<=N;++i)
head[i]=-,vis[i]=,cir[i]=;
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} void dfs(int u)
{
cir[u]=; for(int i=head[u];i!=-;i=E[i].next)
if(!cir[E[i].to])
dfs(E[i].to);
} bool SPFA(int lowcost[],int N,int start)
{
queue <int> que;
int u,v,c; for(int i=;i<=N;++i)
lowcost[i]=INF,couNode[i]=;
lowcost[start]=; que.push(start);
vis[start]=;
couNode[start]=; while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=; // !!! for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(cir[v])
continue; if(lowcost[v]>lowcost[u]+c)
{
lowcost[v]=lowcost[u]+c; if(!vis[v])
{
vis[v]=;
que.push(v); ++couNode[v]; if(couNode[v]>N)
dfs(v);
}
}
}
} return ;
} int ans[MaxN];
int val[MaxN]; inline int cube(int x)
{
return x*x*x;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,Q,M;
int a,b;
int cas=; scanf("%d",&T); while(T--)
{
scanf("%d",&N);
init(N); for(int i=;i<=N;++i)
scanf("%d",&val[i]); scanf("%d",&M);
while(M--)
{
scanf("%d %d",&a,&b);
addEdge(a,b,cube(val[b]-val[a]));
} SPFA(ans,N,); scanf("%d",&Q); printf("Case %d:\n",cas++); while(Q--)
{
scanf("%d",&a); if(cir[a] || ans[a]< || ans[a]==INF)
printf("?\n");
else
printf("%d\n",ans[a]);
}
} return ;
}
(简单) LightOJ 1074 Extended Traffic,SPFA+负环。的更多相关文章
- LightOJ - 1074 Extended Traffic (SPFA+负环)
题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)
题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- SPFA(负环) LightOJ 1074 Extended Traffic
题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...
- LightOJ - 1074 Extended Traffic(标记负环)
题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...
随机推荐
- web项目的集成测试:模拟点击
利用模拟点击的方式进行集成测试,我们准备用google,Firefox和ie三种浏览器测试. 其中除了Firefox是模拟点击的鼻祖不需要插件外,谷歌和ie需要另外下载插件 chromedriver. ...
- Anton and School
Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- OpenGL---------光照的基本知识
从生理学的角度上讲,眼睛之所以看见各种物体,是因为光线直接或间接的从它们那里到达了眼睛.人类对于光线强弱的变化的反应,比对于颜色变化的反应来得灵敏.因此对于人类而言,光线很大程度上表现了物体的立体感. ...
- HttpServletResponse对象
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们 ...
- contentHorizontalAlignment 属性浅析
转载自:http://blog.csdn.net/s0228g0228/article/details/46832285 最近在iOS 7以上总是碰到导航条上左右按钮距离边距太大的问题 为了解决这个这 ...
- python 基本的序列和映射规则
>>> def checkIndex(key):... if not isinstance(key,(int,long)):raise TypeError... if ...
- java 实例变量和类变量的区别
Example4_10.java public class Example4_10 { public static void main(String args[]) { Lader.下底=100; / ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
- HDU - 1083 Courses /POJ - 1469
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://poj.org/problem?id=1469 题意:给你P个课程,并且给出每个课 ...
- Java 多态,重载,重写
1.多态(polymorphism): 多态是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象, ...