3259 Wormholes 之 SPFA
来源:互联网 发布:招聘php程序员要求 编辑:程序博客网 时间:2024/05/24 06:29
为了找到一个更快的方法,我决定是一下SPFA,用的是数据结构课本上的邻接表
time=172ms (用scanf)
- #include <stdio.h>
- #include <memory.h>
- #include <iostream>
- using namespace std;
- #define MAX_NODE 501
- #define INFINITY 999999999
- typedef struct ArcNode{
- int adjvex;
- struct ArcNode *nextarc;
- int weight;
- }ArcNode;
- typedef struct VNode{
- ArcNode *firstarc;
- }VNode;
- VNode *AdjList;
- bool BF(int n,int s)
- {
- bool in[MAX_NODE];
- int count[MAX_NODE];
- ArcNode *p;
- memset(in,false,MAX_NODE*sizeof(bool));
- int Queue[MAX_NODE],d[MAX_NODE];
- for(int i = 1; i <= n; i++) {
- d[i] = INFINITY;
- }
- memset(count,0,(n+1)*sizeof(int));
- int point = 0,u,v;
- Queue[point++] = s;
- in[s] = true;
- d[s] = 0;
- while( point )
- {
- u = Queue[--point];
- in[u] = false;
- count[u]++;
- if(count[u] > n)
- return false;
- p = AdjList[u].firstarc;
- while(p) {
- v = p->adjvex;
- if(d[u] + p->weight < d[v]) {
- if(!in[v]) {
- Queue[point++] = v;
- in[v] = true;
- }
- d[v] = d[u] + p->weight;
- }
- p = p->nextarc;
- }
- }
- return true;
- }
- int main()
- {
- int n,m,w,time;
- int x,y,xy;
- ArcNode *p;
- //freopen("wormhole.7.in","r",stdin);
- //cin >> time;
- scanf("%d",&time);
- while(time--) {
- scanf("%d%d%d",&n,&m,&w);
- //cin >> n >> m >> w;
- AdjList = new VNode[n+1];
- AdjList[0].firstarc = NULL;
- for(int i = 1; i <= n; i++)
- AdjList[i].firstarc = NULL;
- for(int i = 0; i < m; i++) {
- //cin >> x >> y >> xy;
- scanf("%d%d%d",&x,&y,&xy);
- p = new ArcNode;
- p->adjvex = y;
- p->weight = xy;
- p->nextarc = AdjList[x].firstarc;
- AdjList[x].firstarc = p;
- p = new ArcNode;
- p->adjvex = x;
- p->weight = xy;
- p->nextarc = AdjList[y].firstarc;
- AdjList[y].firstarc = p;
- }
- for(int i = 0; i < w; i++) {
- //cin >> x >> y >> xy;
- scanf("%d%d%d",&x,&y,&xy);
- p = new ArcNode;
- p->weight = 0;
- p->adjvex = x;
- p->nextarc = AdjList[0].firstarc;
- AdjList[0].firstarc = p;
- p = new ArcNode;
- p->adjvex = y;
- p->weight = -xy;
- p->nextarc = AdjList[x].firstarc;
- AdjList[x].firstarc = p;
- }
- if(!BF(n,0))
- printf("YES/n");
- else
- printf("NO/n");
- }
- return 0;
- }
最后用了一个类似于邻接矩阵的方法把边储存起来,但是发现还是有点慢,用了141ms,跟没用spfa没快太多,可能是我的数据结构还是不够好吧
- #include <stdio.h>
- #include <memory.h>
- #include <iostream>
- using namespace std;
- #define MAX_NODE 501
- #define INFINITY 999999999
- typedef struct ArcNode{
- int v;
- int next;
- int weight;
- }ArcNode;
- ArcNode a[5500];
- int first[501];
- bool BF(int n,int s)
- {
- bool in[MAX_NODE];
- int count[MAX_NODE];
- memset(in,false,MAX_NODE*sizeof(bool));
- int Queue[MAX_NODE],d[MAX_NODE];
- for(int i = 1; i <= n; i++) {
- d[i] = INFINITY;
- }
- memset(count,0,(n+1)*sizeof(int));
- int point = 0,u,v,p;
- Queue[point++] = s;
- in[s] = true;
- d[s] = 0;
- while( point )
- {
- u = Queue[--point];
- in[u] = false;
- count[u]++;
- if(count[u] > n)
- return false;
- p = first[u];
- while(p) {
- v = a[p].v;
- if(d[u] + a[p].weight < d[v]) {
- if(!in[v]) {
- Queue[point++] = v;
- in[v] = true;
- }
- d[v] = d[u] + a[p].weight ;
- }
- p = a[p].next;
- }
- }
- return true;
- }
- int main()
- {
- int n,m,w,time;
- int x,y,xy,el;
- ArcNode *p;
- scanf("%d",&time);
- while(time--) {
- el = 1;
- scanf("%d%d%d",&n,&m,&w);
- memset(first,0,(n+1)*sizeof(int));
- for(int i = 0; i < m; i++) {
- scanf("%d%d%d",&x,&y,&xy);
- a[el].weight = xy;
- a[el].v = y;
- a[el].next = first[x];
- first[x] = el++;
- a[el].weight = xy;
- a[el].v = x;
- a[el].next = first[y];
- first[y] = el++;
- }
- for(int i = 0; i < w; i++) {
- scanf("%d%d%d",&x,&y,&xy);
- a[el].weight = -xy;
- a[el].v = y;
- a[el].next = first[x];
- first[x] = el++;
- a[el].weight = 0;
- a[el].v = x;
- a[el].next = first[0];
- first[0] = el++;
- }
- if(!BF(n,0))
- printf("YES/n");
- else
- printf("NO/n");
- }
- return 0;
- }
- 3259 Wormholes 之 SPFA
- poj 3259 Wormholes //SPFA
- poj 3259 Wormholes SPFA
- POJ 3259 Wormholes(SPFA)
- Wormholes - POJ 3259 spfa
- poj 3259 Wormholes(spfa)
- POJ 3259:Wormholes 【SPFA】
- Wormholes POJ 3259【SPFA】
- poj 3259-- Wormholes(SPFA)
- POJ-3259 Wormholes(SPFA)
- Poj 3259 Wormholes ( SPFA
- POJ 3259 Wormholes spfa
- [POJ 3259] Wormholes [SPFA]
- pku 3259 Wormholes(SPFA判负环)
- poj 3259 Wormholes spfa算法
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- sql server数据类型char和nchar,varchar和nvarchar,text和ntext的区别
- Warning: The XDE Component XDE Data Modeler has failed to load correctly.
- Java时间操作及格式化
- cluster服务器群的高手们
- 深入浅出C# 中文版 图文皆译 第四章 类型和引用 page153
- 3259 Wormholes 之 SPFA
- 学习编程应该先从那里开始?
- 用jquery+CSS实现左右选择框效果
- excel导入数据库:SQL Server数据库中成批导入数据的几个常用方法
- JDBC
- 实现守护进程的步骤
- 守护进程(daemon)介绍
- ----01-05Socket编程记要
- Strategy模式