Problem Statement

There are $N$ towns numbered $1,\ldots,N$ and $M$ roads numbered $1,\ldots,M$.

Road $i$ connects towns $A_i$ and $B_i$. When you use a road, your score changes as follows:

  • when you move from town $A_i$ to town $B_i$ using road $i$, your score increases by $C_i$; when you move from town $B_i$ to town $A_i$ using road $i$, your score decreases by $C_i$.

Your score may become negative.

Answer the following $Q$ questions.

  • If you start traveling from town $X_i$ with initial score $0$, find the maximum possible score when you are at town $Y_i$.

    Here, if you cannot get from town $X_i$ to town $Y_i$, print nan instead; if you can have as large a score as you want when you are at town $Y_i$, print inf instead.

Constraints

  • $2\leq N \leq 10^5$
  • $0\leq M \leq 10^5$
  • $1\leq Q \leq 10^5$
  • $1\leq A_i,B_i,X_i,Y_i \leq N$
  • $0\leq C_i \leq 10^9$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$ $M$ $Q$
$A_1$ $B_1$ $C_1$
$\vdots$
$A_M$ $B_M$ $C_M$
$X_1$ $Y_1$
$\vdots$
$X_Q$ $Y_Q$

Output

Print $Q$ lines as specified in the Problem Statement.

The $i$-th line should contain the answer to the $i$-th question.


Sample Input 1

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

Sample Output 1

-2
inf
nan

For the first question, if you use road $5$ to move from town $5$ to town $3$, you can have a score $-2$ when you are at town $3$.

Since you cannot make the score larger, the answer is $-2$.

For the second question, you can have as large a score as you want when you are at town $2$ if you travel as follows:
repeatedly "use road $2$ to move from town $1$ to town $2$ and then use road $1$ to move from town $2$ to town $1$" as many times as you want,
and finally use road $2$ to move from town $1$ to town $2$.

For the third question, you cannot get from town $3$ to town $1$.


Sample Input 2

2 1 1
1 1 1
1 1

Sample Output 2

inf

The endpoints of a road may be the same, and so may the endpoints given in a question.


Sample Input 3

9 7 5
3 1 4
1 5 9
2 6 5
3 5 8
9 7 9
3 2 3
8 4 6
2 6
4 3
3 8
3 2
7 9

Sample Output 3

inf
nan
nan
inf
-9

nan 明显就是不同连通块的情况,而当且仅当一个连通块中存在的环都是0环,他这个连通块的点的答案才不是 inf。

但是怎么判断一个连通块是否存在非0环呢?其实可以从某一个点开始搜索,如果到达点 \(x\) 存在两条长度不相等的路径,那么就一定存在非0环。否则就无环或者只有0环。

那么现在已经确定了起始点到某个点的距离了,设起始点为 \(a\) 到点 \(x\) 距离为 \(dis_x\),点 \(x\) 到 点 \(y\) 的距离易得为 \(dis_y-dis_x\)。这是因为没有0环,所有 \(x\) 到 \(y\) 的路径都是同样距离,,当中存在一条路径为 \(x\rightarrow a\rightarrow y\)。

#include<bits/stdc++.h>
typedef long long LL;
const int N=1e5+5;
struct edge{
int v,nxt,w;
}e[N<<1];
int n,m,q,u,v,w,fa[N],hd[N],e_num,vs[N];
LL dp[N];
void add_edge(int u,int v,int w)
{
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
void dfs(int x,LL w)
{
if(dp[x]==dp[0])
dp[x]=w;
else
{
if(dp[x]!=w)
vs[find(x)]=1;
return;
}
for(int i=hd[x];i;i=e[i].nxt)
dfs(e[i].v,w+e[i].w);
}
int main()
{
memset(dp,-0x7f,sizeof(dp));
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,-w);
fa[find(u)]=find(v);
}
for(int i=1;i<=n;i++)
if(fa[i]==i)
dfs(i,0);
while(q--)
{
scanf("%d%d",&u,&v);
if(find(u)!=find(v))
printf("nan\n");
else if(vs[find(u)])
printf("inf\n");
else
printf("%lld\n",dp[v]-dp[u]);
}
}

[ABC280F] Pay or Receive的更多相关文章

  1. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  2. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  3. The trouble of Xiaoqian

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  4. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  5. POJ3260:The Fewest Coins(混合背包)

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  6. hdu 3591 多重加完全DP

    题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

  8. HDUOJ-----3591The trouble of Xiaoqian

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

  10. SOJ 2749_The Fewest Coins

    [题意]:已知整个交易系统有N (1 ≤ N ≤ 100)种不同的货币,分别价值V1,V2,V3.......VN(1 ≤ Vi ≤ 120),FJ分别有C1,C2,C3.....CN(0 ≤ Ci ...

随机推荐

  1. 《Hadoop3.X大数据开发实战(视频教学版)》新书来啦!

  2. Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例

    Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例 点击封面跳转到Unity国际版下载页面 简介 在Unity中, ...

  3. 小知识:OCI实例的私钥文件权限

    在OCI上创建新的实例时,会提示你保存私钥用于连接,而且该界面不会再次显示,所以一定要保存好这个私钥. 实例创建完成后,当使用保存的私钥进行连接时,却发现由于私钥文件的权限问题无法连接,查看当前私钥文 ...

  4. 深入解析HTTP请求:了解请求特征与报文格式的关键秘密

    引言 在上一章节中,我们详细探讨了超文本传输协议(HTTP)的基本概念,并且延伸讨论了HTTP请求响应的基本流程.在这个过程中,浏览器首先通过DNS解析来确定要访问的服务器的IP地址,然后与服务器建立 ...

  5. 【解惑】时间规划,Linq的Aggregate函数在计算会议重叠时间中的应用

    在繁忙的周五,小悦坐在会议室里,面前摆满了各种文件和会议安排表.她今天的工作任务是为公司安排下周的50个小会议,这让她感到有些头疼.但是,她深吸了一口气,决定耐心地一个一个去处理. 首先,小悦仔细地收 ...

  6. Graph RAG: 知识图谱结合 LLM 的检索增强

    本文为大家揭示 NebulaGraph 率先提出的 Graph RAG 方法,这种结合知识图谱.图数据库作为大模型结合私有知识系统的最新技术栈,是 LLM+ 系列的第三篇,加上之前的图上下文学习.Te ...

  7. 4款.NET开源的Redis客户端驱动库

    前言 今天给大家推荐4款.NET开源免费的Redis客户端驱动库(以下排名不分先后). Redis是什么? Redis全称是REmote DIctionary Service,即远程字典服务.Redi ...

  8. 关于 React 性能优化和数栈产品中的实践

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:的卢 引入 在日常开发过程中,我们会使用很多性能优化的 A ...

  9. 手把手教你在项目中引入Excel报表组件

    摘要:本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 GrapeCity Documents for Excel(以下 ...

  10. 为zabbix穿上一件漂亮的外衣

    zabbix+Grafana 7.0  zabbix的环境已部署好的情况下,zabbix部分-- 略 Grafana简介: 1.Grafana自身并存储数据,数据从其它地方获取.需要配置数据源 2.G ...