• P1993 小K的农场


    题面

    https://www.luogu.org/problem/P1993

    题解

    那一天好像收到了我女神的$qq$信息呢,女神鼓励我辽~~~

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstdlib>
    #include<map>
    
    using namespace std;
    int n,m,dis[10500],vis[10500];
    vector <int> to[10500],l[10500];
    map<int,int> oto[10500];
    
    void add(int u,int v,int c) {
      if (oto[u].count(v)) l[u][oto[u][v]]=min(c,l[u][oto[u][v]]);
      else {
        to[u].push_back(v);
        l[u].push_back(c);
        oto[u][v]=to[u].size()-1;
      }
    }
    
    void dfs(int x,int col){
      vis[x]=col;
      int i,siz=to[x].size();
      for (i=0;i<siz;i++) {
        if (vis[to[x][i]]==0) {
          dis[to[x][i]]=dis[x]+l[x][i];
          dfs(to[x][i],col); 
        }
        else 
          if (vis[to[x][i]]!=col) continue;
        else {
          if (dis[x]+l[x][i]<0) {
            puts("No");
            exit(0);
          }
        }
      }
    }
    
    int main(){
      int i,opt,a,b,c;
      scanf("%d %d",&n,&m);
      for (i=1;i<=m;i++) {
        scanf("%d",&opt);
        if (opt==1) {
          scanf("%d %d %d",&a,&b,&c);
          add(a,b,-c);
          //to[a].push_back(b);
          //l[a].push_back(-c);
        }
        else if (opt==2) {
          scanf("%d %d %d",&a,&b,&c);
          add(b,a,c);
          //to[b].push_back(a);
          //l[b].push_back(c);
        }
        else {
          scanf("%d %d",&a,&b);
          add(a,b,0);
          add(b,a,0);
          //to[a].push_back(b);
          //l[a].push_back(0);
          //to[b].push_back(a);
          //l[b].push_back(0);
        }
      }
      for (i=1;i<=n;i++) if (vis[i]==0) {
        dis[i]=0;
        dfs(i,i);
      }
      puts("Yes");
    }
  • 相关阅读:
    HDU 1584 蜘蛛牌(DFS)
    HDU 1800 Flying to the Mars(贪心)
    zsh: command not found: java (xxx)
    Deepin20安装Mysql8
    Deepin全局菜单
    Ubuntu PPA 解读
    Node安装与配置
    Windows安装配置Maven
    idea 安装 Vue 插件没有Vue component选项
    Linux桌面系统创建应用程序快捷方式
  • 原文地址:https://www.cnblogs.com/shxnb666/p/11427440.html
Copyright © 2020-2023  润新知