题解

3 条题解

  • 1
    @ 2020-11-19 17:18:19

    很水的差分約束系統的題目

    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <limits>
    using namespace std;
    
    namespace dts
    {
        typedef long long ll;
        const ll oo_min=0xcfcfcfcfcfcfcfcf,oo_max=0x3f3f3f3f3f3f3f3f;
        
        ll n,m;
        vector<ll> a,b,c,vis,cnt,dist;
        vector<vector<ll>> e,l;
        deque<ll> q;
        
        void bfs_resize(ll size)
        {
            vis.resize(size,0),cnt.resize(size,0);
            dist.resize(size,oo_min);
            e.resize(size),l.resize(size);
            for (ll i=0;i<size;i++)
                e[i].clear(),l[i].clear();
            q.clear();
        }
        
        void add_edge(ll x,ll y,ll d)
        {
            e[x].push_back(y);
            l[x].push_back(d);
        }
        
        ll bfs()
        {
            dist[0]=0;
            for (vis[0]=1,cnt[0]++,q.push_back(0);!q.empty();vis[q.front()]=0,q.pop_front())
            {
                ll now=q.front();
                for (ll i=0;i<e[now].size();i++)
                    if (dist[now]+l[now][i]>dist[e[now][i]])
                    {
                        dist[e[now][i]]=dist[now]+l[now][i];
                        if (vis[e[now][i]]==0)
                        {
                            if (cnt[e[now][i]]>=n)
                                return -1;
                            else
                            {
                                vis[e[now][i]]=1,cnt[e[now][i]]++;
                                q.push_back(e[now][i]);
                            }
                        }
                        else if (now==e[now][i])
                            return -1;
                    }
            }
            ll ans=0;
            for (ll i=1;i<=n;i++)
                ans+=dist[i];
            return ans;
        }
        
        void main()
        {
            while (~scanf("%lld%lld",&n,&m))
            {
                a.resize(m,0),b.resize(m,0),c.resize(m,0);
                for (ll i=0;i<m;i++)
                    scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
                bfs_resize(n+1);
                for (ll i=1;i<=n;i++)
                    add_edge(0,i,0);
                for (ll i=0;i<m;i++)
                    add_edge(a[i],b[i],c[i]);
                printf("%lld\n",bfs());
            }
        }
    }
    
    int main()
    {
        dts::main();
    }
    
    • @ 2020-11-19 23:20:01

      這個題目,有一個未說明的條件:如果c為負數,則將c修改為0。

      #include <cmath>
      #include <cstdio>
      #include <cstdlib>
      #include <cstring>
      #include <algorithm>
      #include <vector>
      #include <deque>
      #include <limits>
      using namespace std;
      
      namespace dts
      {
          typedef long long ll;
          typedef unsigned long long ull;
          
          ull n,m;
          vector<ull> a,b,c,vis,cnt,dist;
          vector<vector<ull>> e,l;
          deque<ull> q;
          
          void bfs_resize(ull size)
          {
              vis.resize(size,0),cnt.resize(size,0);
              dist.resize(size,0);
              e.resize(size),l.resize(size);
              for (ull i=0;i<size;i++)
                  e[i].clear(),l[i].clear();
              q.clear();
          }
          
          void add_edge(ull x,ull y,ull d)
          {
              e[x].push_back(y);
              l[x].push_back(d);
          }
          
          void bfs()
          {
              for (ull i=1;i<=n;i++)
                  vis[i]=1,cnt[i]++,q.push_back(i);
              for (;!q.empty();vis[q.front()]=0,q.pop_front())
              {
                  ull now=q.front();
                  for (ull i=0;i<e[now].size();i++)
                      if (dist[now]+l[now][i]>dist[e[now][i]])
                      {
                          dist[e[now][i]]=dist[now]+l[now][i];
                          if (vis[e[now][i]]==0)
                          {
                              if (cnt[e[now][i]]>=n)
                                  goto no_solution;
                              else
                              {
                                  vis[e[now][i]]=1,cnt[e[now][i]]++;
                                  q.push_back(e[now][i]);
                              }
                          }
                          else if (now==e[now][i])
                          {
                              no_solution:
                              printf("-1\n");
                              return;
                          }
                      }
              }
              ull ans=0;
              for (ull i=1;i<=n;i++)
                  ans+=dist[i];
              printf("%llu\n",ans);
          }
          
          void main()
          {
              while (~scanf("%llu%llu",&n,&m))
              {
                  a.resize(m,0),b.resize(m,0),c.resize(m,0);
                  for (ull i=0;i<m;i++)
                  {
                      char sa[1<<5],sb[1<<5],sc[1<<5];
                      memset(sa,0,sizeof(sa)),memset(sb,0,sizeof(sb)),memset(sc,0,sizeof(sc));
                      scanf("%s%s%s\n",sa,sb,sc);
                      if (sc[0]=='-')
                          sscanf(sa,"%llu",&a[i]),sscanf(sb,"%llu",&b[i]),c[i]=0;
                      else
                          sscanf(sa,"%llu",&a[i]),sscanf(sb,"%llu",&b[i]),sscanf(sc,"%llu",&c[i]);
                  }
                  bfs_resize(n+1);
                  for (ull i=1;i<=n;i++)
                      add_edge(0,i,0);
                  for (ull i=0;i<m;i++)
                      add_edge(a[i],b[i],c[i]);
                  bfs();
              }
          }
      }
      
      int main()
      {
          dts::main();
      }
      
  • 1
    @ 2020-03-31 13:47:59

  • -1
    @ 2015-11-02 12:53:27

    hhhhhhh

  • 1

信息

ID
1972
难度
8
分类
(无)
标签
递交数
60
已通过
8
通过率
13%
被复制
3
上传者