• POJ 2457 Part Acquisition


      第一反应是BFS,比较直观,但是输出路径写的不是很熟练,此外,习惯不好,“==”写成了“=”,所以常量一定放前面!

     1 #include <cstdio>
     2 #include <queue>
     3 #include <cstring>
     4 using namespace std;
     5 int N, K;
     6 typedef struct node
     7 {
     8     int in, out;
     9     int pos;
    10 }Link;
    11 const int maxn = 50005;
    12 bool vis[maxn];
    13 Link A[maxn];
    14 int pre[maxn], B[maxn];
    15 
    16 int main(void) {
    17     freopen("in.txt", "r", stdin);
    18     freopen("out.txt", "w", stdout);
    19     scanf("%d%d", &N, &K);
    20     memset(vis, false, sizeof(vis));
    21     queue<Link> Q;
    22     for (int i = 0; i < N; i++) {
    23         scanf("%d%d", &A[i].in, &A[i].out);
    24         A[i].pos = i;
    25         if (A[i].in == 1) {
    26             pre[i] = -1;
    27             vis[i] = true;
    28             Q.push(A[i]);
    29         }
    30     }
    31 
    32     if (K == 1) {
    33         printf("1
    1
    ");
    34     } else {
    35         if (Q.empty()) {
    36             printf("-1
    ");
    37         } else {
    38             while (!Q.empty()) {
    39                 Link front = Q.front();
    40                 if (front.out == K) {
    41                     break;
    42                 }
    43                 for (int i = 0; i < N; i++) {
    44                     if (!vis[i] && A[i].in == front.out) {
    45                         Q.push(A[i]);
    46                         vis[i] = true;
    47                         pre[i] = front.pos;
    48                     }
    49                 }
    50                 Q.pop();
    51 
    52             }
    53             if (Q.empty()) {
    54                 printf("-1
    ");
    55             } else {
    56                 int cnt = 0;
    57                 int i = Q.front().pos;
    58                 for (;-1 != pre[i];) {
    59                     B[cnt++] = A[i].out;
    60                     i = A[pre[i]].pos;
    61                 }
    62                 B[cnt++] = A[i].out;
    63 
    64                 printf("%d
    1
    ", cnt+1);
    65                 for (int k = cnt-1; k >= 0; k--) {
    66                     printf("%d
    ", B[k]);
    67                 } 
    68             }
    69         }
    70     }
    71     
    72 } 
    View Code

      看到别人用SPFA、dijkstra,有时间也试一试!

      【待写】

  • 相关阅读:
    sql优化
    mysql_存储过程_后一行减去前一行
    python基础笔记
    atom使用markdown
    tensorboard遇到的坑
    WordPaster-Firefox浏览器控件安装方法
    WordPaster.exe安装教程
    Web大文件上传控件-bug修复-Xproer.HttpUploader6
    Web大文件下载控件更新-Xproer.HttpDownloader
    Web大文件上传控件-asp.net-bug修复-Xproer.HttpUploader6.2
  • 原文地址:https://www.cnblogs.com/zhaoyu1995/p/5765702.html
Copyright © 2020-2023  润新知