Problem Statement

We have a directed graph with \(N\) vertices and \(M\) edges. Edge \(i\) is directed from Vertex \(A_i\) to \(B_i\).and has a weight of \(C_i\).

Initially, there is a piece on Vertex \(v\). Takahashi and Aoki will play a game where they alternate turns moving the piece as follows:

  • If there is no edge that goes from the vertex on which the piece is placed, end the game.
  • If there are edges that go from the vertex on which the piece is placed, choose one of those edges and move the piece along that edge.

Takahashi goes first. Takahashi tries to minimize the total weight of the edges traversed by the piece, and Aoki tries to maximize it.

More formally, their objectives are as follows.

Takahashi gives the first priority to ending the game in a finite number of moves. If this is possible, he tries to minimize the total weight of the edges traversed by the piece.

Aoki gives the first priority to preventing the game from ending in a finite number of moves. If this is impossible, he tries to maximize the total weight of the edges traversed by the piece.

(If the piece traverses the same edge multiple times, the weight is added that number of times.)

Determine whether the game ends in a finite number of moves when both players play optimally. If it ends, find the total weight of the edges traversed by the piece.

Constraints

  • \(1≤N≤2×10^5\)
  • \(0≤M≤2×10^5\)
  • \(1\le v≤N\)
  • \(1≤A_i,B_i≤N\)
  • There is no multi-edges. That is, \((A_i,B_i)\ne (A_j,B_j)\) for \(i\ne j\)
  • There is no self-loops. That is, \(A_i\ne B_i\).
  • \(0≤C≤10 ^9\)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

\(N\) \(M\) \(v\)

\(A_1\) \(B_1\) \(C_1\)

\(A_2\) \(B_2\) \(C_2\)



\(A_M\) \(B_M\) \(C_M\)

Output

If the game does not end in a finite number of moves when both players play optimally, print INFINITY.

If the game ends in a finite number of moves, print the total weight of the edges traversed by the piece.

Sample Input 1

7 6 1
1 2 1
1 3 10
2 4 100
2 5 102
3 6 20
3 7 30

Sample Output 1

40

First, Takahashi will move the piece to Vertex \(3\). Next, Aoki will move the piece to Vertex \(7\), and the game will end.

The total weight of the edges traversed by the piece will be \(10+30=40\).

Sample Input 2

3 6 3
1 2 1
2 1 2
2 3 3
3 2 4
3 1 5
1 3 6

Sample Output 2

INFINITY

The game will not end in a finite number of moves.

Sample Input 3

4 4 1
1 2 1
2 3 1
3 1 1
2 4 1

Sample Output 3

5

The piece will go \(1→2→3→1→2→4\).

假设图是一个有向无环图,那么直接使用 DAG 上 dp 即可。轮到 Alice 时在所有后继节点中取最小值。轮到 Bob 时在所有后继节点中取最大值。

那么如果是有环呢?有环的 min/max dp 的经典解法是跑最短路。把无后继的节点入堆,图建成反图,跑 dij 就行了。

但是这个 dp 有时候取最大值,有时候取最小值,好像很难处理。

首先如果轮到 Alice 的话,直接取最小值。因为 dij 的堆用小根堆,所以Alice的dp值直接跑dij是正确的。

但是如果轮到 Bob,他的值不像 Alice,需要所有的后继节点更新后,才能更新 Bob 的dp值。而且他是希望可以在博弈中跑出死循环的,所以如果不是所有的后继节点都更新得到,他就不更新。我们可以使用类似拓扑的方式处理。记录入度,然后松弛时入度减一。

那么想清楚代码就很好写了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+5;
struct node{
int v,k;
LL w;
bool operator<(const node&n)const{
return w>n.w;
}
};
struct edge{
int v,nxt,w;
}e[N<<1];
int hd[N],n,m,s,u,v,w,e_num,in[N],vis[N][2];
LL dis[N][2];
priority_queue<node>q;
void add_edge(int u,int v,int w)
{
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
void dijkstra()
{
for(int i=1;i<=n;i++)
{
if(!in[i])
{
q.push((node){i,0,0});
q.push((node){i,1,0});
}
else
dis[i][0]=0x7f7f7f7f7f7f7f7f;
}
// printf("%d\n",dis[1][0]);
while(!q.empty())
{
int v=q.top().v,k=q.top().k;
// printf("%d %d %d\n",v,k,dis[v][k]);
q.pop();
if(vis[v][k])
continue;
if(k)
{
for(int i=hd[v];i;i=e[i].nxt)
{
// if(v==2)
// printf("%d %d %d\n",e[i].v,dis[v][1]+e[i].w,dis[e[i].v][0]);
if(dis[v][1]+e[i].w<dis[e[i].v][0])
{
dis[e[i].v][0]=dis[v][1]+e[i].w;
q.push((node){e[i].v,0,dis[e[i].v][0]});
}
}
}
else
{
for(int i=hd[v];i;i=e[i].nxt)
{
in[e[i].v]--;
if(dis[v][0]+e[i].w>dis[e[i].v][1])
{
dis[e[i].v][1]=dis[v][0]+e[i].w;
// q.push((node){e[i].v,1,-dis[e[i].v][1]});
}
if(!in[e[i].v])
q.push((node){e[i].v,1,dis[e[i].v][1]});
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w),in[u]++;
add_edge(v,u,w);
}
dijkstra();
if(dis[s][0]>1e18)
printf("INFINITY");
else
printf("%lld",dis[s][0]);
}

[ABC261Ex] Game on Graph的更多相关文章

  1. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  2. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  5. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  9. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  10. 纸上谈兵: 图 (graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

随机推荐

  1. 错过这5大AI绘画提示词平台,你会拍大腿!别问,直接收藏!

    如今,AI绘画已经不再是简单的技术展示,而是逐渐转向了商业化的运营. 有的人利用AI生成的图片,再结合ChatGPT产生的文字,然后在平台上发布,这样就可以赚取平台的广告费. 其他一些变现操作参考之前 ...

  2. QEMU tap数据接收流程

    QEMU直接从tap/tun取数据 QEMU tap数据接收步骤: qemu从tun取数据包 qemu将数据包放入virtio硬件网卡. qemu触发中断. 虚拟机收到中断,从virtio读取数据. ...

  3. AI绘图开源工具Stable Diffusion WebUI前端API对接

    背景 本文主要介绍 AI 绘图开源工具 Stable Diffusion WebUI 的 API 开启和基本调用方法,通过本文的阅读,你将了解到 stable-diffusion-webui 的基本介 ...

  4. 代码随想录算法训练营第二十五天| 216.组合总和III 17.电话号码的字母组合

      216.组合总和III 卡哥建议:如果把 组合问题理解了,本题就容易一些了. 题目链接/文章讲解:https://programmercarl.com/0216.%E7%BB%84%E5%90%8 ...

  5. 文盘Rust——起手式,CLI程序

    技术的学习从不会到会的过程是最有意思的,也是体会最多的.一旦熟练了,知识变成了常识,可能就失去了记录学习过程的最佳时机. 在我看来学习一门计算机语言和学习人类语言有很多共通之处.我们学习人类语言是从单 ...

  6. Unity 游戏开发、01 基础知识大全、简单功能脚本实现

    2.3 窗口布局 Unity默认窗口布局 Hierarchy 层级窗口 Scene 场景窗口,3D视图窗口 Game 游戏播放窗口 Inspector 检查器窗口,属性窗口 Project 项目窗口 ...

  7. PLC通过Modbus转Profinet网关与合康变频器Modbus通讯案例

    PLC通过Modbus转Profinet网关(XD-MDPN100)与合康变频器Modbus通讯,实现了两个设备之间的数据交互.Profinet是一种基于以太网的实时工控网络协议,而Modbus是一种 ...

  8. fatal: 无法访问 'https://github.com/nmww/lingyun.git/':Failed to connect to github.com port 443 after 13 ms: Connection refused

    fatal: 无法访问 'https://github.com/nmww/lingyun.git/':Failed to connect to github.com port 443 after 13 ...

  9. 创建及管理DSW实例

      机器学习PAI 产品概述 快速入门 操作指南 准备工作 工作空间管理 AI计算资源管理 AI开发 开发流程 快速开始 智能标注(iTAG) 可视化建模(PAI-Designer) 交互式建模(PA ...

  10. 流水线中便捷迭代,鲲鹏DevKit 23.0新能力抢先看

    本文分享自华为云社区<鲲鹏DevKit 23.0:流水线中便捷迭代鲲鹏版本,迁移.开发.调优无缝衔接>,作者:华为云社区精选 . 数字时代,海量的行业应用驱动着多样性算力的飞速发展,以鲲鹏 ...