UVALIVE 3307 Adventurous Driving
一开始重新建图搞的。然后参照了别人的博客。这个解法比较好
利用在SPFA维护入队次数。入队次数大于节点数目的位于负环。
那么负环中的点如何DFS到终点。(SPFA从起点开始如果能找到入队大于N那么一定可以从起点到这个点)那么就NOBOUND;
VOID和输出ANS就比较容易了
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 2000
#define MAXM 20010
const int INF = 0x3f3f3f3f;
int N,M,A,B;
struct node
{
int u,v,next;
int length,weight;
}edge[MAXM];
int head[MAXN],cnt;
int num[MAXN],d[MAXN],l[MAXN];
bool inq[MAXN],used[MAXN];
int q[MAXM];
bool found;
void insert(int u ,int v,int weight,int length)
{
if (head[u] != - && weight > edge[head[u]].weight) return;
if (head[u] != - && weight < edge[head[u]].weight) head[u] = -;
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].weight = weight;
edge[cnt].length = length;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void read()
{
memset(head,-,sizeof(head));
cnt = ;
for (int i = ; i < M; i++)
{
int u ,v, weightl,weightr,length;
// if (i != M - 1)
scanf("(%d,%d,%d[%d]%d) ",&u,&v,&weightl,&length,&weightr);
// else scanf("(%d,%d,%d[%d]%d)",&u,&v,&weightl,&length,&weightr);
//printf("%d %d %d %d %d \n",u,v,weightl,length,weightr);
insert(u,v,weightl,length);
insert(v,u,weightr,length);
}
}
bool SPFA(int s)
{
int front = ,rear = ;
memset(inq,false,sizeof(inq));
memset(num,,sizeof(num));
memset(d,0x3f,sizeof(d));
memset(l,0x3f,sizeof(l));
inq[s] = true;
q[] = s;
d[s] = l[s] = ;
num[s] = ;
while (front !=rear)
{
int u = q[front] ;
front = (front + ) % MAXM;
inq[u] = false;
for (int i = head[u]; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if (d[v] > d[u] + edge[i].weight ||( (d[v] == d[u] + edge[i].weight) && (l[v] > l[u] + edge[i].length)))
{
d[v] = d[u] + edge[i].weight;
l[v] = l[u] + edge[i].length;
if (!inq[v])
{
inq[v] = true;
num[v]++;
q[rear] = v;
rear = (rear + ) % MAXM;
if (num[v] > N + ) return false;
}
}
}
}
return true;
}
void dfs(int cur)
{
used[cur] = true;
if (cur == B) {found = true; return;}
for (int i = head[cur]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (!used[v]) dfs(v);
}
}
int main()
{
// freopen("sample.txt","r",stdin);
while (scanf("%d%d%d%d ",&N,&M,&A,&B) != EOF)
{
found = false;
read();
bool flag = SPFA(A);
if (d[B] == INF) puts("VOID");
else
{
if (flag) printf("%d %d\n",d[B],l[B]);
else
{
for (int i = ; i < N; i++)
if (num[i] > N)
{
memset(used,false,sizeof(used));
found = false;
dfs(i);
if (found) break;
}
if (found || num[B] > N) puts("UNBOUND");
else printf("%d %d\n",d[B],l[B]);
}
}
}
return ;
}
UVALIVE 3307 Adventurous Driving的更多相关文章
- POJ 2679:Adventurous Driving(SPFA+DFS)
http://poj.org/problem?id=2679 Adventurous Driving Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2679 Adventurous Driving(SPFA 负环)
/* - - 这题做了一天.....粗心害死人啊 题目描述恶心 数据更恶心... 先处理一下能走的边 能走的点(到这建边从终点跑一下.) 然后就是SPFA了 注意负环的判断 */ #include&l ...
- UVALive 4868 Palindrometer 暴力
F - Palindrometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- mac系统下mysql开机启动总是3307
修改了mysql的my.cnf可还是不行,启动后就是3307,必须关掉再启动. 觉得可能是mac系统在哪里写死了开机启动项. http://queforum.com/mysql/1012987-mys ...
- tensorfolw配置过程中遇到的一些问题及其解决过程的记录(配置SqueezeDet: Unified, Small, Low Power Fully Convolutional Neural Networks for Real-Time Object Detection for Autonomous Driving)
今天看到一篇关于检测的论文<SqueezeDet: Unified, Small, Low Power Fully Convolutional Neural Networks for Real- ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
随机推荐
- Java并发基础--线程通信
java中实现线程通信的四种方式 1.synchronized同步 多个线程之间可以借助synchronized关键字来进行间接通信,本质上是通过共享对象进行通信.如下: public class S ...
- LeetCode - 20. Valid Parentheses(0ms)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Python 3 学习笔记之——面向对象
1. 类的介绍 类(Class) 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例,类是对象的抽象. 方法:类中定义的函数. 类变量:类变量在整个实 ...
- Java开发JDBC连接数据库
Java开发JDBC连接数据库 创建一个以JDBC连接数据库的程序,包含6个步骤: JDBC五部曲1.加载驱动2.获得链接3.获取statement对象 4.执行SQL语句5.产生resultset对 ...
- JQuery JTable根据某行的某个值来设置行的背景颜色
目录 描述 处理方法 参考 描述 某个表的数据是用JQuery的JTable插件进行展示的.现在需求是:当表中的master字段为true时,就将对应的整行的背景颜色设置为浅蓝色. 处理方法 在fie ...
- lintcode-91-最小调整代价
91-最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 注意事项 你可以假设数组 ...
- lintcode-99-重排链表
99-重排链表 给定一个单链表L: L0→L1→-→Ln-1→Ln, 重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→- 必须在不改变节点值的情况下进行原地操作. 样例 给出链表 1-> ...
- Apache服务器的Options 的 Indexes FollowSymLinks详解
禁止显示Apache目录列表 - Indexes FollowSymLinks 如何修改目录的配置以禁止显示 Apache 目录列表. 缺省情况下如果你在浏览器输入地址: http://localho ...
- oracle分区技术提高查询效率
概述: 当表中的数据量不断增大,查询数据的速度就会变慢,应用程序的性能就会下降,这时就应该考虑对表进行分区.表进行分区后,逻辑上表仍然是一张完整的表,只是将表中的数据在物理上存放到多个表空间(物理文件 ...
- BZOJ4419 SHOI2013发微博(平衡树)
好友状态的变化次数不会超过m,于是考虑暴力,对每个人记录其好友关系的变化,通过前缀和计算贡献.这需要查询一段前缀时间内某人发的微博数量,可以离线建一棵绝对平衡的平衡树.事实上完全可以线性. #incl ...