跳到正文

题解

ABC261Ex Game on Graph 题解

Coffee_zzz约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

定义 fu,0f_{u,0} 表示 Takahashi 在点 uu 先手时的得分,fu,1f_{u,1} 表示 Aoki 在点 uu 先手时的得分。

初始时,所有出度为 00 的结点 uu 所对应的 fu,0f_{u,0} 和 fu,1f_{u,1} 的值均为 00。为了方便,令出度不为 00 的结点 uu 所对应的 fu,0f_{u,0} 的值为极大值,并令其对应的 fu,1f_{u,1} 的值为 00。

当图是 DAG 时,容易得到转移方程:

fu,0=min⁡(u,v)∈Efv,1+w(u,v)fu,1=max⁡(u,v)∈Efv,0+w(u,v)f_{u,0}=\min_{(u,v)\in E} f_{v,1}+w(u,v)\\ f_{u,1}=\max_{(u,v)\in E} f_{v,0}+w(u,v)\\

接下来考虑怎么处理环。

由于 Takahashi 的目标是最小化得分,所以 fu,0f_{u,0} 可以直接用 Dijkstra 计算;而 Aoki 的目标是最大化得分,如果有无限循环的方法一定会选择无限循环,所以只有点 uu 的所有出边都被处理过时才能更新 fu,1f_{u,1} 的值,在判断后进行转移即可。

计算完成后,若 fs,0f_{s,0} 为极大值则游戏无法结束,否则 fs,0f_{s,0} 即为答案。

时间复杂度 O(mlog⁡m)\mathcal O (m \log m)。

C++
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define i128 __int128
#define endl '\n'
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define vei vector<int>
#define pq priority_queue
#define yes puts("yes")
#define no puts("no")
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#define In(x) freopen(x".in","r",stdin)
#define Out(x) freopen(x".out","w",stdout)
#define File(x) (In(x),Out(x))
using namespace std;
const int N=2e5+5,inf=4e18;
int n,m,s,deg[N],f[N][2];
vector <pii> ve[N];
bool vis[N][2];
struct Nod{
	int u,t,w;
};
bool operator > (Nod a,Nod b){
	return a.w>b.w;
}
pq <Nod,vector<Nod>,greater<Nod> > q;
void solve(){
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		int u,v,w;
		cin>>u>>v>>w;
		deg[u]++;
		ve[v].pb({w,u});
	}
	for(int u=1;u<=n;u++){
		if(deg[u]) f[u][0]=inf;
		else q.push({u,0,0}),q.push({u,1,0});
	}
	while(!q.empty()){
		int u=q.top().u,t=q.top().t;
		q.pop();
		if(vis[u][t]) continue;
		vis[u][t]=1;
		for(auto p:ve[u]){
			int w=p.fi,v=p.se;
			if(t==0){
				deg[v]--;
				f[v][1]=max(f[v][1],f[u][0]+w);
				if(deg[v]==0) q.push({v,1,f[v][1]});
			}
			else if(f[v][0]>f[u][1]+w){
				f[v][0]=f[u][1]+w;
				q.push({v,0,f[v][0]});
			}
		}
	}
	if(f[s][0]==inf) cout<<"INFINITY"<<endl;
	else cout<<f[s][0]<<endl;
}
signed main(){
	ios::sync_with_stdio(0);
	signed T=1;
//	cin>>T;
	while(T--) solve();
	return 0;
}
原文