zoukankan      html  css  js  c++  java
  • YSU小吃街

    • 贪心贪不过,暴力搜就完事了
    • 注意不连通情况
    • #include<iostream>
      #include<sstream>
      #include<cstdio>
      #include<cstdlib>
      #include<string>
      #include<cstring>
      #include<algorithm>
      #include<functional>
      #include<iomanip>
      #include<numeric>
      #include<cmath>
      #include<queue>
      #include<vector>
      #include<set>
      #include<map>
      #include<cctype>
      const double PI = acos(-1.0);
      const int INF = 0x3f3f3f3f;
      const int NINF = -INF - 1;
      const int maxn = 1e5 + 5;
      typedef long long ll;
      #define MOD 1000000007
      using namespace std;
      int n, m, st;
      int vis[maxn];
      int rec = 0;
      typedef pair<int, int> P;
      struct node
      {
          int next, cost;
          int flag;
      };
      vector<node> G[maxn];
      void bfs()
      {
          queue<P> q;
          q.push(P(1, st));
          while(q.size())
          {
              P p = q.front();
              q.pop();
              for(int i = 0; i < G[p.first].size(); ++i)
              {
                  node tmp = G[p.first][i];
                  int mp;
                  if(!tmp.flag) mp = p.second - tmp.cost;
                  else mp = p.second + tmp.cost;
                  if (vis[tmp.next] == -1) {
                      vis[tmp.next] = mp;
                      q.push(P(tmp.next, vis[tmp.next]));
                  }
                  else{
                      if(vis[tmp.next] != mp) {
                          rec = 1;
                          break;
                      }
                  }
              }
              if(rec) break;
          }
      }
      int main()
      {
          memset(vis, -1, sizeof(vis));
          scanf("%d %d %d", &n, &m, &st);
          for(int i = 0; i < m; ++i)
          {
              int x, y, w;
              scanf("%d %d %d", &x, &y, &w);
              G[x].push_back(node{y, w, 0});
              G[y].push_back(node{x, w, 1});
          }
          vis[1] = st;
          bfs();
      //    for(int i = 1; i <= n; ++i)
      //    {
      //        cout << vis[i] << ' ';
      //    }
      //    cout << endl;
          for(int i = 1; i <= n; ++i)
          {
              if(vis[i] == -1)
              {
                  rec = 1;
                  break;
              }
          }
          if(rec)
          {
              cout << "QAQ";
          }
          else
          {
              cout << "QWQ" << endl;
              for(int i = 1; i <= n; ++i)
              {
                  cout << vis[i] << endl;
              }
          }
          return 0;
      }
  • 相关阅读:
    django_开发报错
    SpringBoot 前后端数据参数交互
    消息队列学习笔记(一)
    2021年调用工商二维码退款查询接口
    2021年调用工商二维码退款接口
    2021年调用工商二维码生成接口及回调接口demo
    调用工商生成二维码接口文档的坑
    使用hutool工具类转换时间
    微信模板消息推送
    pom文件 spring-boot-maven-plugin 爆红
  • 原文地址:https://www.cnblogs.com/veasky/p/11984995.html
Copyright © 2011-2022 走看看