[ABC280F] Pay or Receive
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$, printnaninstead; if you can have as large a score as you want when you are at town $Y_i$, printinfinstead.
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的更多相关文章
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- The trouble of Xiaoqian
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- POJ3260:The Fewest Coins(混合背包)
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- hdu 3591 多重加完全DP
题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 3591 (完全背包+二进制优化的多重背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...
- HDUOJ-----3591The trouble of Xiaoqian
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- SOJ 2749_The Fewest Coins
[题意]:已知整个交易系统有N (1 ≤ N ≤ 100)种不同的货币,分别价值V1,V2,V3.......VN(1 ≤ Vi ≤ 120),FJ分别有C1,C2,C3.....CN(0 ≤ Ci ...
随机推荐
- python机器学习经典算法代码示例及思维导图(数学建模必备)
最近几天学习了机器学习经典算法,通过此次学习入门了机器学习,并将经典算法的代码实现并记录下来,方便后续查找与使用. 这次记录主要分为两部分:第一部分是机器学习思维导图,以框架的形式描述机器学习开发流程 ...
- [ABC305C] Snuke the Cookie Picker题解
题目大意 有一个 \(H\times W\) 的网格,一种有一个矩形,矩形中间有一个点被挖空,求这个点的坐标.(. 表示空白,# 表示矩形内的点) 解析 观察我们可以发现,每一矩形内的个点上下左右至少 ...
- 你能看到这个汉字么“ ” ?关于Unicode的私人使用区(PUA) 和浏览器端显示处理
如果你现在使用的是chrome查看那么你是看不到我标题中的汉字的,显示为一个小方框,但是你使用edge查看的话,这个字就能正常的显示出来,不信你试试! 本故事源于我在做数据过程中遇到Unicode编码 ...
- CodeForces 1367D Task On The Board
题意 给一个字符串\(t\),和一个长度为\(m\)的数组\(b[]\),要求构造一个字符串\(s\),\(s\)中的字符都出现在\(t\)中,对于\(s[i]\)而言,对于任意\(j\),如果有\( ...
- 【krpano】KRPano测试开发专用浏览器
KRPano技术解密网站:http://www.krpano.tech 在开发KRPano项目时候,每次测试时均需要打开测试服务器,或者上传到空间中才能进行测试,十分不方便. 因此本站提供了免费的KR ...
- java数组的定义和使用规范
java数组 三种定义方式 1.数组类型[] 数组名字 = new 数组类型[数组长度] String[] str = new String[n]; //这里n代表数组的长度可变 //另外上面这种写法 ...
- Springboot+Mybatisplus+ClickHouse集成
核心依赖引入 <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clic ...
- c语言代码练习15
//使用togo跳转代码,阻止关机#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> ...
- How to start with Gradle?
How to start with Gradle? Download the latest Gradle release from http://www.gradle.org/downloads Se ...
- 研发三维GIS系统笔记/实现wgs84投影-002
四叉树代码修改完善 原来的代码中,没有使用投影转换,直接使用的是世界坐标(单位是米), CELLQuadTree::CELLQuadTree( CELLTerrainInterface* pInter ...