3 条题解
-
1Sky1231 (sky1231) LV 10 @ 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(); }
-
12020-03-31 13:47:59@
-
-12015-11-02 12:53:27@
hhhhhhh
- 1
信息
- ID
- 1972
- 难度
- 8
- 分类
- (无)
- 标签
- 递交数
- 60
- 已通过
- 8
- 通过率
- 13%
- 被复制
- 3
- 上传者