SalN @ 2023-07-11 18:42:22
60pts,o2 就 WA,否则 RE+TLE。
#include<bits/stdc++.h>
#define pii pair<int,int>
#define N 1510
#define M 5010
#define pb push_back
#define pi make_pair
using namespace std;
const int P=1e9+7;
int n, m, vis[N], d[N], u[M], v[M], w[M];
int inz[N], inf[N], ans[N], f[N], g[N];
vector<int> to[N], eg[N], ez[N], ef[N];
priority_queue<pii> q;
queue<int> qwq, awa;
void solve(int s) {
memset(inz,0,sizeof(inz));
memset(inf,0,sizeof(inf));
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
while(q.size()) q.pop();
while(qwq.size()) qwq.pop();
while(awa.size()) awa.pop();
for(int i=1; i<=n; ++i)
ez[i].clear(), ef[i].clear();
d[s]=0, q.push(pi(0,s));
while(q.size()) {
int x=q.top().second; q.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=0; i<to[x].size(); ++i) {
int y=to[x][i], v=eg[x][i];
if(vis[y]) continue;
if(d[x]+v<d[y]) {
d[y]=d[x]+v;
q.push(pi(-d[y],y));
}
}
}
for(int i=1; i<=m; ++i)
if(d[u[i]]+w[i]==d[v[i]]) {
ez[u[i]].pb(v[i]);
ef[v[i]].pb(u[i]);
inz[v[i]]++, inf[u[i]]++;
}
for(int i=1; i<=n; ++i) {
if(inz[i]==0) qwq.push(i);
if(inf[i]==0) awa.push(i);
}
f[s]=1;
while(qwq.size()) {
int x=qwq.front(); qwq.pop();
for(int i=0; i<ez[x].size(); ++i) {
int y=ez[x][i];
f[y]=(f[x]+f[y])%P;
if(--inz[y]==0) qwq.push(y);
}
}
for(int i=1; i<=n; ++i) g[i]=1;
while(awa.size()) {
int x=awa.front(); awa.pop();
for(int i=0; i<ef[x].size(); ++i) {
int y=ef[x][i];
g[y]=(g[x]+g[y])%P;
if(--inf[y]==0) awa.push(y);
}
}
for(int i=1; i<=m; ++i)
if(d[u[i]]+w[i]==d[v[i]]) {
int lsy=f[u[i]]*g[v[i]]%P;
ans[i]=(ans[i]+lsy)%P;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=1; i<=m; ++i) {
cin >> u[i] >> v[i] >> w[i];
to[u[i]].pb(v[i]);
eg[u[i]].pb(w[i]);
}
for(int i=1; i<=n; ++i) solve(i);
for(int i=1; i<=m; ++i)
cout << ans[i] << endl;
return 0;
}
by SalN @ 2023-07-11 18:52:21
我看不出来哪里错了。哈哈!!
by 北文 @ 2023-07-11 18:57:55
@ChElsYqwq 别急我帮你
by 北文 @ 2023-07-11 18:58:04
@ChElsYqwq 但是我得先看题目/
by SalN @ 2023-07-11 18:59:41
@北文 谢谢学长()
by Meardoe @ 2023-07-11 19:30:17
变量名好萌(?)
by 北文 @ 2023-07-11 20:04:31
@ChElsYqwq 年老力衰 自己都调不出来,在等我半小时吧
by SalN @ 2023-07-11 20:08:28
@北文 好的学长!
by 北文 @ 2023-07-11 20:10:52
@ChElsYqwq 终于过了
by 北文 @ 2023-07-11 20:11:48
@ChElsYqwq 一眼ans开小
by 北文 @ 2023-07-11 20:13:34
@ChElsYqwq 不要乱用memset在有需要的时候清0不然会浪费很多时间,开o2能过,就是因为你用了太多memset而有的是没必要的