3259 Wormholes 之 SPFA

来源:互联网 发布:招聘php程序员要求 编辑:程序博客网 时间:2024/05/24 06:29

为了找到一个更快的方法,我决定是一下SPFA,用的是数据结构课本上的邻接表

time=172ms (用scanf)

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <iostream>
  4. using namespace std;
  5. #define MAX_NODE 501
  6. #define INFINITY 999999999
  7. typedef struct ArcNode{
  8.     int adjvex;
  9.     struct ArcNode *nextarc;
  10.     int weight;
  11. }ArcNode;
  12. typedef struct VNode{
  13.     ArcNode *firstarc;
  14. }VNode;
  15. VNode *AdjList;
  16. bool BF(int n,int s)
  17. {
  18.     bool in[MAX_NODE];
  19.     int count[MAX_NODE];
  20.     ArcNode *p;
  21.     memset(in,false,MAX_NODE*sizeof(bool));
  22.     int Queue[MAX_NODE],d[MAX_NODE];
  23.     for(int i = 1; i <= n; i++) {
  24.         d[i] = INFINITY;
  25.     }
  26.     memset(count,0,(n+1)*sizeof(int));
  27.     int point = 0,u,v;
  28.     Queue[point++] = s;
  29.     in[s] = true;
  30.     d[s] = 0;
  31.     while( point )
  32.     {
  33.         u = Queue[--point];
  34.         in[u] = false;
  35.         count[u]++;
  36.         if(count[u] > n)
  37.             return false;
  38.         p = AdjList[u].firstarc;
  39.         while(p) {
  40.             v = p->adjvex;
  41.             if(d[u] + p->weight < d[v]) {
  42.                 if(!in[v]) {
  43.                     Queue[point++] = v;
  44.                     in[v] = true;
  45.                 }
  46.                 d[v] = d[u] + p->weight;
  47.             }
  48.             p = p->nextarc;
  49.         }
  50.     }
  51.     return true;
  52. }
  53. int main()
  54. {
  55.     int n,m,w,time;
  56.     int x,y,xy;
  57.     ArcNode *p;
  58.     //freopen("wormhole.7.in","r",stdin);
  59.     //cin >> time;
  60.     scanf("%d",&time);
  61.     while(time--) {
  62.         scanf("%d%d%d",&n,&m,&w);
  63.         //cin >> n >> m >> w;
  64.         AdjList = new VNode[n+1];
  65.         AdjList[0].firstarc  = NULL;
  66.         for(int i = 1; i <= n; i++) 
  67.             AdjList[i].firstarc = NULL;
  68.         for(int i = 0; i < m; i++) {
  69.             //cin >> x >> y >> xy;
  70.             scanf("%d%d%d",&x,&y,&xy);
  71.             p = new ArcNode;
  72.             p->adjvex = y;
  73.             p->weight = xy;
  74.             p->nextarc = AdjList[x].firstarc;
  75.             AdjList[x].firstarc = p;
  76.             p = new ArcNode;
  77.             p->adjvex = x;
  78.             p->weight = xy;
  79.             p->nextarc = AdjList[y].firstarc;
  80.             AdjList[y].firstarc = p;
  81.         }
  82.         for(int i = 0; i < w; i++) {
  83.             //cin >> x >> y >> xy;
  84.             scanf("%d%d%d",&x,&y,&xy);
  85.             p = new ArcNode;
  86.             p->weight = 0;
  87.             p->adjvex = x;
  88.             p->nextarc = AdjList[0].firstarc;
  89.             AdjList[0].firstarc = p;
  90.             p = new ArcNode;
  91.             p->adjvex = y;
  92.             p->weight = -xy;
  93.             p->nextarc = AdjList[x].firstarc;
  94.             AdjList[x].firstarc = p;
  95.         }
  96.         if(!BF(n,0))
  97.             printf("YES/n");
  98.         else
  99.             printf("NO/n");
  100.     }
  101.         return 0;
  102. }

最后用了一个类似于邻接矩阵的方法把边储存起来,但是发现还是有点慢,用了141ms,跟没用spfa没快太多,可能是我的数据结构还是不够好吧

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <iostream>
  4. using namespace std;
  5. #define MAX_NODE 501
  6. #define INFINITY 999999999
  7. typedef struct ArcNode{
  8.     int v;
  9.     int next;
  10.     int weight;
  11. }ArcNode;
  12. ArcNode a[5500];
  13. int first[501];
  14. bool BF(int n,int s)
  15. {
  16.     bool in[MAX_NODE];
  17.     int count[MAX_NODE];
  18.     memset(in,false,MAX_NODE*sizeof(bool));
  19.     int Queue[MAX_NODE],d[MAX_NODE];
  20.     for(int i = 1; i <= n; i++) {
  21.         d[i] = INFINITY;
  22.     }
  23.     memset(count,0,(n+1)*sizeof(int));
  24.     int point = 0,u,v,p;
  25.     Queue[point++] = s;
  26.     in[s] = true;
  27.     d[s] = 0;
  28.     while( point )
  29.     {
  30.         u = Queue[--point];
  31.         in[u] = false;
  32.         count[u]++;
  33.         if(count[u] > n)
  34.             return false;
  35.         p = first[u];
  36.         while(p) {
  37.             v = a[p].v;
  38.             if(d[u] + a[p].weight  < d[v]) {
  39.                 if(!in[v]) {
  40.                     Queue[point++] = v;
  41.                     in[v] = true;
  42.                 }
  43.                 d[v] = d[u] + a[p].weight ;
  44.             }
  45.             p = a[p].next;
  46.         }
  47.     }
  48.     return true;
  49. }
  50. int main()
  51. {
  52.     int n,m,w,time;
  53.     int x,y,xy,el;
  54.     ArcNode *p;
  55.     scanf("%d",&time);
  56.     while(time--) {
  57.         el = 1;
  58.         scanf("%d%d%d",&n,&m,&w);
  59.         memset(first,0,(n+1)*sizeof(int));
  60.         for(int i = 0; i < m; i++) {
  61.             scanf("%d%d%d",&x,&y,&xy);
  62.             a[el].weight = xy;
  63.             a[el].v = y;
  64.             a[el].next = first[x];
  65.             first[x] = el++;
  66.             a[el].weight = xy;
  67.             a[el].v = x;
  68.             a[el].next = first[y];
  69.             first[y] = el++;
  70.         }
  71.         for(int i = 0; i < w; i++) {
  72.             scanf("%d%d%d",&x,&y,&xy);
  73.             a[el].weight = -xy;
  74.             a[el].v = y;
  75.             a[el].next = first[x];
  76.             first[x] = el++;
  77.             a[el].weight = 0;
  78.             a[el].v = x;
  79.             a[el].next = first[0];
  80.             first[0] = el++;
  81.         }
  82.         if(!BF(n,0))
  83.             printf("YES/n");
  84.         else
  85.             printf("NO/n");
  86.     }
  87.         return 0;
  88. }