ABC261Ex Game on Graph 题解
Coffee_zzz · · 题解
定义
初始时,所有出度为
当图是 DAG 时,容易得到转移方程:
接下来考虑怎么处理环。
由于 Takahashi 的目标是最小化得分,所以
计算完成后,若
时间复杂度
#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;
}