hdu4725 The Shortest Path in Nya Graph

首先感谢prey大爷提供的脑洞和特殊的dijkstra姿势

题意就不说了..主要注意题目有问题,输入的是m条边,愣是写的n条边[还想为什么有个输入的值完全没用到呢....

在这里放上prey帮调题的时候写的数据生成器

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100233;
 4 int perm[maxn];
 5 vector<int> G[maxn];
 6 int main(int argc, char ** argv)
 7 {
 8     assert(argc == 5);
 9     int n = atoi(argv[1]), m = atoi(argv[2]),
10         lim = atoi(argv[3]), c = atoi(argv[4]),  seed;
11     FILE *rnd = fopen("/dev/urandom", "r");
12     fread(&seed, 4, 1, rnd); fclose(rnd); 
13     mt19937 RAND(seed);
14        printf("1\n%d %d %d\n", n, m, c);
15     for(int i = 1; i <= n; i++)
16         printf("%d ", int(RAND()%(n) + 1));
17     printf("\n"); 
18     set<pair<int, int> > S;
19     for(int i = 1; i <= m; i++)
20     {
21         int u, v;
22         do u = int(RAND() % n + 1), v =  int(RAND() % n + 1);
23         while (u == v || S.count(make_pair(u, v)) || S.count(make_pair(v, u)));
24         S.insert(make_pair(u, v));
25         printf("%d %d %d\n", u, v, int(RAND()%lim));
26     }
27 }
数据生成器

首先编译数据生成器(gen.cpp)

g++ gen.cpp -std=c++11 -o gen

再编译ac程序 

g++ acc.cpp -o acc

从in里读取输入文件

./acc <in

然后编译自己的程序

g++ 2.cpp -o waa

./waa <in

最后执行这句话 前面的数字代表给数据生成器提供的参数,这里代表的事n,m,mod,c

while ./gen 100000 100000 1000 2333 > in && ./acc < in > ac && ./waa < in > wa && diff -w ac wa;do echo OK;done

最后放上拥有特殊姿势的dijkstra

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <vector>
  4 #include <queue>
  5 #define maxn 300010
  6 #define maxm 600010
  7 #define INF 200000000
  8 #include <algorithm>
  9 using namespace std;
 10 vector <unsigned > fr[maxn];
 11 struct dij{
 12     unsigned d, u; dij() {}
 13     inline bool operator<(const dij& other) const { return d > other.d; }
 14     inline dij(unsigned _d, unsigned _u) : d(_d), u(_u) {} 
 15 };
 16 struct heap
 17 {
 18     unsigned  size;
 19     dij data[maxm];
 20     inline void push(dij x) { data[size++] = x; push_heap(data, data + size); }
 21     inline dij top()  { return *data; }
 22     inline void pop() { pop_heap(data, data + size--); }
 23 }Q;
 24 struct ed{
 25     unsigned u, v, w, next;
 26 }a[maxm];
 27 unsigned n, m, c, tot;
 28 unsigned be[maxn], first[maxn], done[maxn];
 29 unsigned dis[maxn];
 30 void addedge(unsigned st, unsigned end, unsigned val)
 31 {
 32     a[++tot].u = st;a[tot].v = end;a[tot].w = val;
 33     a[tot].next =first[st];first[st] = tot;
 34 }
 35 inline void DIJKSTRA()
 36 {
 37     for(unsigned i = 1; i <=3*n; i++)dis[i] = INF, done[i] = 0;
 38     Q.push(dij(dis[1] = 0, 1));
 39     while (Q.size)
 40     {
 41         unsigned u = Q.top().u; Q.pop();
 42         if(done[u])continue;
 43         done[u] = 1;
 44         for(unsigned e = first[u]; e; e = a[e].next)
 45         {
 46             unsigned v = a[e].v;
 47             if(dis[u] + a[e].w < dis[v])
 48             {
 49                 dis[v] = dis[u] + a[e].w;    
 50                 Q.push(dij(dis[v], v));
 51             }
 52         }
 53     }
 54 
 55 }
 56 template<class T> inline void maxt(T& A, T b) { if (A < b) A = b; }
 57 //template<class T> inline bool mint(T& a, T b) { return a > b ? a = b, true : false; }
 58 template<class T> inline void mint(T& A, T b) { if (A > b) A = b; }
 59 //if (mint(dist[v], dist[u] + e[ee].w) && !inq[v])
 60 //    inq[que[qt++] = v] = true; prwang两行spfa
 61 int main()
 62 {
 63     unsigned T, x, y, z;
 64     scanf("%u", &T);
 65     for(unsigned t = 1; t <= T; t++)    
 66     {
 67         memset(first, 0, sizeof(first));
 68         memset(a, 0, sizeof(a));
 69         memset(be, 0, sizeof(be));
 70         tot = 0;
 71         scanf("%u %u %u", &n, &m, &c);
 72         for(unsigned i = 1; i <= n; i++)fr[i].clear();
 73         unsigned  lmin = n, lmax = 1;
 74         for(unsigned i = 1; i <= n; i++)
 75         {
 76             scanf("%u ", &be[i]);
 77             fr[be[i]].push_back(i);
 78             maxt(lmax, be[i]), mint(lmin, be[i]);
 79         }
 80         for(unsigned i = 1; i <= m; i++)
 81         {
 82             scanf("%u %u %u", &x, &y, &z);
 83             addedge(x, y, z);addedge(y, x, z);
 84         }
 85         unsigned cc = n + 1;
 86         for (unsigned i = lmin, j = i + 1; i < lmax; i = j++)
 87         {
 88             while (!fr[j].size())  ++j;
 89             if (j - i > 1) continue;
 90             for(unsigned k = 0; k < (unsigned)fr[i].size(); k++)
 91                 addedge(fr[i][k], cc, 0);
 92             for(unsigned k = 0; k < (unsigned)fr[j].size(); k++)
 93                 addedge(cc, fr[j][k], c*(j-i));
 94             cc++;
 95             for(unsigned k = 0; k < (unsigned)fr[i].size(); k++)
 96                 addedge(cc, fr[i][k], 0);
 97             for(unsigned k = 0; k < (unsigned)fr[j].size(); k++)
 98                 addedge(fr[j][k], cc, c*(j-i));
 99             cc++;
100         }
101         DIJKSTRA();
102         if(dis[n] == INF)
103             printf("Case #%u: -1\n", t);
104         else 
105         printf("Case #%u: %u\n", t, dis[n]);
106     }
107 }

 

posted @ 2015-09-03 15:38  z52527  阅读(214)  评论(0编辑  收藏  举报