lightoj 1074
这题怎么说呢,负环上的点都不行
网上也有很多解法
我用dfs的spfa解的
我发现网上别人的代码都是用bfs的spfa写的,我就用的dfs的,快了好多

代码还看的别人的,只有中间的spfa是自己写的
我自己原来写的不知道为什么就过不了
把别人的spfa换成自己的就过了
这个是ac的:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std; const int N = ;
const int MAX = 0x3f3f3f3f;
int dis[N];
int p[N];
int h[N];
int cnt[N];
bool vis[N];
bool r[N];
int c[N]; struct Node
{
int u, v, w, next;
}e[N*N]; void dfs(int x)
{
r[x] = ;
for(int i=h[x]; i!=-; i=e[i].next)
if(!r[e[i].v])
dfs(e[i].v);
} void spfa(int u)
{
if(r[u])
return ;
vis[u] = true;
for(int i = h[u]; i != -; i = e[i].next)
{
int v,w;
v = e[i].v;
w = e[i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u]+w;
if(vis[v])
{
dfs(v);
return ;
}
else
{
spfa(v);
}
}
}
vis[u] = false;
} int main()
{
int T;
scanf("%d", &T);
for(int tt=; tt<=T; tt++)
{
int n, m;
scanf("%d", &n);
memset(cnt, , sizeof(cnt));
memset(vis, , sizeof(vis));
memset(r, , sizeof(r));
memset(dis, 0x3f3f3f3f, sizeof(dis));
for(int i=; i<=n; i++)
scanf("%d", &p[i]);
scanf("%d", &m);
memset(h, -, sizeof(h));
for(int i=; i<m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
e[i].u = a;
e[i].v = b;
e[i].w = (p[b]-p[a])*(p[b]-p[a])*(p[b]-p[a]);
e[i].next = h[a];
h[a] = i;
}
int Q;
scanf("%d", &Q);
for(int i=; i<Q; i++)
scanf("%d", &c[i]);
printf("Case %d:\n", tt); dis[] = ;
spfa();
for(int i=; i<Q; i++)
{
int to = c[i];
if(dis[to]==MAX || r[to] || dis[to] < )
printf("?\n");
else
printf("%d\n", dis[to]);
}
}
return ;
}
这个是wa的,那位dalao帮我抓抓虫啊,真的苦,我看了半天楞是没看出来
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n,m;
int cnt;
struct node
{
int v,w;
};
bool vis[maxn];
int head[maxn];
int nex[maxn];
node e[maxn*maxn];
int dis[maxn];
int busy[maxn];
bool vis2[maxn];
int c[maxn];
void adde(int u,int v,int w)
{
node t;
t.v = v;
t.w = w;
e[cnt] = t;
nex[cnt] = head[u];
head[u] = cnt ++;
}
void spfa(int u);
void dfs(int u);
int main()
{
int t;
scanf("%d",&t);
for(int k = ; k <= t; ++k)
{
memset(vis2,,sizeof(vis2));
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
//memset(cn,0,sizeof(cn));
memset(nex,-,sizeof(nex));
memset(e,,sizeof(e));
cnt = ;
scanf("%d",&n);
for(int i = ; i <= n; ++i)
scanf("%d",busy+i); scanf("%d",&m);
for(int i = ; i < m; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
int c = (busy[b] - busy[a])*(busy[b] - busy[a])*(busy[b] - busy[a]);
adde(a,b,c);
}
int Q;
scanf("%d",&Q);
for(int i = ; i < Q; ++i)
scanf("%d",c+i); printf("Case %d:\n", k);
dis[] = ;
spfa();
for(int i = ; i < Q; ++i)
{
if(dis[c[i]] == INF || vis2[c[i]] || dis[c[i]] < )
printf("?\n");
else
printf("%d\n",dis[c[i]]);
}
}
return ;
}
void spfa(int u)
{
if(vis2[u])
return ;
vis[u] = true;
for(int i = head[u]; i != -; i = nex[i])
{
int v,w;
v = e[i].v;
w = e[i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u]+w;
if(vis[v])
{
dfs(v);
return ;
}
else
{
spfa(v);
}
}
}
vis[u] = false;
}
void dfs(int u)
{
vis2[u] = true;
for(int i = head[u]; i != -; i = nex[i])
{
int v = e[i].v;
if(vis2[v] == false)
dfs(v);
}
}
lightoj 1074的更多相关文章
- lightoj 1074 spfa判断负环
Extended Traffic Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Sub ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- 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的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...
- kuangbin_ShortPath O (LightOJ 1074)
这是什么鬼OJ啊都没见过害的我还交错语言CE了一发摔 想着懒得重写了直接把上一题的dij改了改就交了 然后RE 反应过来这题有负环 想着怎么标记负环同时不直接结束spfa 看了别人的代码感叹了一下我还 ...
- 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 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- (简单) LightOJ 1074 Extended Traffic,SPFA+负环。
Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...
随机推荐
- Trie树详解(转)
特别声明 本文只是一篇笔记类的文章,所以不存在什么抄袭之类的. 以下为我研究时参考过的链接(有很多,这里我只列出我记得的): Trie(字典树)的应用——查找联系人 trie树 Trie树:应用于统计 ...
- Ubuntu 16.04 上安装 PCL 1.8.0
Ubuntu16.04之后安装pcl可以直接apt-get sudo apt-get install libpcl-dev pcl-tools 安装之前,准备一些依赖库 sudo apt-get up ...
- 关于jqGrid组件数据显示不出问题
jqGrid组件一开始怎么数据都返回了渲染不出来,查找了一天,最后发现点击搜索之后doSearch()事件触发的方法并不是数据请求接口,而是再次请求了初次登录的接口,从初次登录返回的数据,数据格式没问 ...
- wpf 寻找TreeView的子元素,并对其进行操作
//itemsControl 开始为指定的TreeView控件 item为TreeView子元素 private void PareItems(ItemsControl itemsControl, ...
- JS判断图片是否加载完成 背景图404 快到碗里来
面对这个问题 我最多做到表面笑嘻嘻 …… 真不知道测试怎么那么…… 啥都能给你测出来 有的没的都能给你测出来 算了算了 谁让本仙女本精灵本可爱温柔大方善解人意呢 …呵呵呵 ————————————正 ...
- 自学elastic search
工作也有一段时间了,虽然来这个公司之后学会了几门不同的语言,但想拨尖还是任重道远. 想往高级程序员甚至是架构师方向发展.他仍然是我的学习对象.我现在做着的,无非是他玩剩下的罢了. luncene之前有 ...
- mRNA文库构建
mRNA文库构建 Posted: 三月 27, 2017 Under: Transcriptomics By Kai no Comments RNA-seq测序方法 在测mRNA过程中,首先要去 ...
- 生信分析常用脚本(二)--SOAPdenovo
1.SOAPDenovo配置文件示例 软件下载安装和使用:http://soap.genomics.org.cn/soapdenovo.html asm.cfg #maximal read lengt ...
- 在IE浏览器进行编辑操作再展示出现乱码问题
解决方法: ajax传输数据时进行encodeURI编码就可以了 例如: 在其他浏览器中没有进行 encodeURI 直接传输,无问题. 然而,在IE10 和11中进行下面这段代码编辑后,再展示出来就 ...
- JS 变量提升与函数提升
JS 变量提升与函数提升 JS变量提升 变量提升是指:使用var声明变量时,JS会将变量提升到所处作用域的顶部.举个简单的例子: 示例1 console.log(foo); // undefined ...