zoukankan      html  css  js  c++  java
  • HDU 4889 Scary Path Finding Algorithm

    其实这个题是抄的题解啦…… 题解给了一个图,按照那个图模拟一遍大概就能理解了。

    题意:

      有一段程序,给你一个C值(程序中某常量),让你构造一组数据,使程序输出"doge"
       那段代码大概是 SPFA的SLF优化。其实题目的意思是让我们构造一组数据,使得总的出队次数大于C。
            数据范围 C<=23,333,333。输出的图中最多有100个点,没有重边、自环、负环。


    思路:

      SLF: 设队首元素为 i, 队列中要加入节点 j, 在        时加到队首而不是队尾, 否则和普通的 SPFA 一样加到队尾.

      这个优化是基于贪心的思想,因为出当前结点的的距离越小,那么他可能更新点就越多,从而达到优化的目的。

      因为是贪心,我们可以“欺骗”他一下。
            我们可以让距离比较大的结点加入队列,那么他会比较晚出队,但是,他会经过一系列的会更新原来更新过的结点,那么被更新的点会重新入队。那么被更新点原来的更新路径会重新被更新一次。

     

    题解:来自这里

    代码: 先在程序中把图建出来,然后在输出图。这样做会简单一些。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cmath>
     6 #include <algorithm>
     7 #include <string>
     8 #include <queue>
     9 #include <stack>
    10 #include <vector>
    11 #include <map>
    12 #include <set>
    13 #include <functional>
    14 #include <time.h>
    15 
    16 using namespace std;
    17 
    18 const int INF = 1<<30;
    19 const int MAXN = 105;
    20 
    21 int myPow(int d, int n) {
    22     int res = 1;
    23     while (n>0) {
    24         if (n&1) res *= d;
    25         d *= d;
    26         n >>= 1;
    27     }
    28     return res;
    29 }
    30 
    31 vector<pair<int, int> > G[MAXN];
    32 int n, m;
    33 
    34 void getGraph() {
    35     for (int i = 0; i < MAXN; i++)
    36         G[i].clear();
    37     n = 1;
    38     for (int i = 30; i >= 0; i--) {
    39         G[n].push_back(make_pair(n+1, 0));
    40         G[n].push_back(make_pair(n+2, (i!=0) ? (-myPow(2, i-1)) : 0));
    41         G[n+1].push_back(make_pair(n+2, -myPow(2, i)));
    42         n += 2;
    43     }
    44     m = 0;
    45     for (int i = 1; i <= n; i++)
    46         m += G[i].size();
    47 }
    48 
    49 void output() {
    50     printf("%d %d
    ", n, m);
    51     for (int i = 1; i <= n; i++)
    52         for (int j = 0; j < G[i].size(); j++)
    53             printf("%d %d %d
    ", i, G[i][j].first, G[i][j].second);
    54 
    55 }
    56 
    57 int main() {
    58     #ifdef Phantom01
    59         freopen("HDU4889.txt", "r", stdin);
    60     #endif //Phantom01
    61 
    62     getGraph();
    63     int C;
    64     while (scanf("%d", &C)!=EOF) {
    65         output();
    66     }
    67 
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    iOS上传AppStore被拒原因及处理方案
    记录一个打包upload时报错处理
    Git撤销merge
    设置UIButton上面是image,下面是title
    Sourcetree中切换到其他分支,临时分支的这次提交丢失
    修改字符串指定范围的颜色
    定时消失的Alert弹窗
    ALAsset 将资源转换为 NSData
    textField和textView的输入字数最大限制
    手机号码正则判断
  • 原文地址:https://www.cnblogs.com/Phantom01/p/3878177.html
Copyright © 2011-2022 走看看