QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#534218#4437. Link with RunningxuzhihaodedieAC ✓733ms99112kbC++202.4kb2024-08-26 22:02:152024-08-26 22:02:15

Judging History

你现在查看的是最新测评结果

  • [2024-08-26 22:02:15]
  • 评测
  • 测评结果:AC
  • 用时:733ms
  • 内存:99112kb
  • [2024-08-26 22:02:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define lson 2*p
#define rson 2*p+1
#define x first
#define y second
const int N=3e5+10;
const int mod=1e9+7;
vector<int> g[N],scc[N];
vector<PII> h[N],G[N];
int n,m;
int tot,num,top,cnt;
int s[N],ins[N],c[N],a[N];
int dfn[N],low[N];
int u[N],v[N],e[N],p[N];
void tarjan(int x) {
	dfn[x]=low[x]=++num;
	s[++top]=x,ins[x]=1;
	for(int y:g[x]) {
		if(!dfn[y]) {
			tarjan(y);
			low[x]=min(low[x],low[y]);
		} else if(ins[y]) {
			low[x]=min(low[x],dfn[y]);
		}
	}
	if(dfn[x]==low[x]) {
		cnt++;
		int y;
		do {
			y=s[top--],ins[y]=0;
			c[y]=cnt,scc[cnt].push_back(y);
		} while(x!=y);
	}
}
void solve() {
    int n,m;
    cin>>n>>m;
    cnt=top=num=tot=0;
    for(int i=0; i<=n; i++) {
        h[i].clear();
        G[i].clear();
		g[i].clear();
		scc[i].clear();
		dfn[i]=low[i]=ins[i]=c[i]=s[i]=0;
	}
    for(int i=1;i<=m;i++) {
        cin>>u[i]>>v[i]>>e[i]>>p[i];
        h[u[i]].push_back({v[i],e[i]});
    }
    priority_queue<PII,vector<PII>,greater<PII> > q;
    vector<int> dist(n+10,1e18),vis(n+10,0);
    dist[1]=0;
    q.push({0,1});
    while(q.size()) {
        auto p1=q.top();
        q.pop();
        int x=p1.second;
        if(vis[x]) continue;
        vis[x]=1;
        for(auto it:h[x]) {
        	int y=it.x,z=it.y;
            if(dist[y]>dist[x]+z) {
                dist[y]=dist[x]+z;
                q.push({dist[y],y});
            }
        }
    }
	cout<<dist[n]<<" ";
	for(int x=1;x<=n;x++) {
		for(auto it:h[x]) {
			int y=it.x,z=it.y;
			if(dist[x]+z==dist[y]) g[x].push_back(y);
		}
	}
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
	queue<int> q1;
	vector<int> dp(n+10,0),deg(n+10,0);
    for(int i=1;i<=m;i++) {
        if(dist[u[i]]+e[i]==dist[v[i]]) {
        	if(c[u[i]]==c[v[i]]) continue;
            G[c[u[i]]].push_back({c[v[i]],p[i]});
            deg[c[v[i]]]++;
        }
    }
    q1.push(c[1]);
    while(q1.size()) {
        int x=q1.front();
        q1.pop();
        //cout<<x<<endl;
        for(auto it:G[x]) {
        	int y=it.x,z=it.y;
            dp[y]=max(dp[y],dp[x]+z);
            deg[y]--;
            if(!deg[y]) q1.push(y);
        }
    }
    cout<<dp[c[n]]<<endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T=1;
	cin>>T;
	while(T--) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 733ms
memory: 99112kb

input:

12
100000 200000
1 2 838279516 902819511
1 3 293478832 513256010
2 4 682688353 204481674
2 5 360092507 651108247
5 6 519851939 323803002
6 7 675439277 205804465
7 8 419167205 386168059
6 9 140767493 382483305
9 10 558115401 613738466
9 11 902235661 744659643
9 12 851394758 1720015
12 13 635355827 46...

output:

5927443549 11285847934
2529348 325344428756
2522027 438209666288
250100947 25049026205784
249512452 24966236662852
0 9535132634
0 25375698217
0 1000000000
0 1
0 2
0 1
0 1

result:

ok 12 lines