首页 > 其他分享 >abc280F - Pay or Receive(判断是否全为零环)

abc280F - Pay or Receive(判断是否全为零环)

时间:2023-11-16 19:24:02浏览次数:29  
标签:puts vis Receive Pay abc280F define int include lld

https://atcoder.jp/contests/abc280/tasks/abc280_f

对于每一个连通块单独处理,首先判断是否全为0环,可以用bfs判断。
从一个点出发计算其他点到它的最短距离,如果存在一个不唯一,说明存在非零环。

然后计算距离的时候直接-d[x]+d[y]即可

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#define A puts("YES")
#define B puts("NO")
//#define A puts("Yes")
//#define B puts("No")
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
#define lc (o<<1)
#define rc (o<<1|1)
using namespace std;
//typedef __int128 i128;
typedef double db;
typedef long long ll;
const ll mo=998244353;
const ll inf=1ll<<60;
const int N=1e6+5;
ll nex[N*2],head[N],to[N*2],tot,w[N*2],d[N],cnt;
ll bz[N],b[N];
ll n,m,Q,x,y,z;
bool vis[N];
queue<int> q;
void add(ll x,ll y,ll z){
	to[++tot]=y; nex[tot]=head[x]; head[x]=tot; w[tot]=z;
}
void bfs(int s){
	while (!q.empty()) q.pop();
	
	b[s]=++cnt;
	q.push(s);
	vis[s]=1;
	while (!q.empty()) {
		x=q.front(); q.pop();
		for (int i=head[x];i;i=nex[i]) {
			int v=to[i];
			if (!vis[v]) {
				vis[v]=1;
				d[v]=d[x]+w[i];
				q.push(v);
				b[v]=cnt;
			}
			if (d[v]!=d[x]+w[i]) bz[cnt]=1;
		}
	}
}
int main()
{
//	freopen("data.in","r",stdin);
//	freopen("ans.out","w",stdout);
	
	scanf("%lld %lld %lld",&n,&m,&Q);
	fo(i,1,m) {
		scanf("%lld %lld %lld",&x,&y,&z);
		add(x,y,z);
		add(y,x,-z);
	}
	
	fo(i,1,n) {
		if (!vis[i]) {
			bfs(i);
		}
	}
	
	
	while (Q--) {
		scanf("%lld %lld",&x,&y);
		if (b[x]^b[y]) puts("nan");
		else if (bz[b[x]]) puts("inf");
		else printf("%lld\n",-d[x]+d[y]);
	}

	return 0;
} 

  

标签:puts,vis,Receive,Pay,abc280F,define,int,include,lld
From: https://www.cnblogs.com/ganking/p/17837063.html

相关文章

  • Spring Boot集成Druid异常discard long time none received connection.
    问题描述解决方案禁用PingMethod,在SpringBoot项目中,可在启动类中添加如下静态代码快:static{System.setProperty("druid.mysql.usePingMethod","false");}......
  • 服务器发送了一个意外的数据包。 received: 3, expected: 20
     [root@node02local]#vim/etc/ssh/sshd_config#最后一行添加[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1[root@node02local]#systemctlreloadsshd ......
  • 微信支付:wxpay.unifiedOrder(data)返回appid 与 openId 不配
    原因:小程序和APP、公众号等支付方式夸端口调用支付,后台配置多个appId时A程序中的openid在B程序中支付。即使用A程序的openid和B程序的appIdy去调用wxpay.unifiedOrder(data)把请求统一支付的参数输出:得到当前的appid,微信返回后看到另一个Appid,如果两个一致,则不会出现不匹配问题......
  • 渗透中 PoC、Exp、Payload、RCE、IOC,Shellcode 的区别
    PoC:全称“ProofofConcept”,中文“概念验证”,常指段漏洞证明的代码。Exp:全称“Exploit”,中文“利用”,指利用系统漏洞进行攻击的动作作。Payload:中文“有效载荷”,指成功exploit之后,真正在目标系统执行的代码或指令RCE:RCE(remotecommand/codeexecute)可以让攻击......
  • 502 Proxy Error The proxy server received an invalid response from an upstream s
    ProxyErrorTheproxyserverreceivedaninvalidresponsefromanupstreamserver.TheproxyservercouldnothandletherequestGET /wpsp/.Reason:ErrorreadingfromremoteserverApache/2.2.15(CentOS)Serveratwww.xaut.edu.cnPort80 解决方法:重启Apache,再......
  • [ABC231E] Minimal payments 题解
    题目传送门一道贪心题。感觉很裸啊,模拟赛时随便乱写了个暴力递归就能过。每次找最接近钱数\(x\)的面额\(num\),如果比钱数少那么答案为剩下\(x\bmodnum\)钱数的答案加上\(x\divnum\)。否则答案则为剩下\(num-x\)钱数的答案加上\(1\)。Code#include<bits/stdc++.h......
  • MQTT控制报文格式 -- PUBREC – Publish received (QoS 2 publish received, part 1)
    PUBREC数据包是对QoS2的PUBLISH数据包的响应。它是QoS2协议交换的第二个数据包。该数据包剩余长度为2该数据包没有Payload该数据包可变包头长度为2个字节1.固定包头FixedHeaderBit76543210byte1MQTTControlPackettyp......
  • MQTT控制报文格式 -- PUBREL – Publish release (QoS 2 publish received, part 2)
    PUBREL数据包是对PUBREC数据包的响应。它是QoS2协议交换的第三个数据包。该数据包剩余长度为2该数据包没有Payload该数据包可变包头长度为2个字节1.固定包头FixedHeaderBit76543210byte1MQTTControlPackettype(6)R......
  • 微信支付 Verify the signature and get the Wechatpay certificate corresponding to
    1.先获取商户证书文件这块叫商户证书需要和下面的支付证书名字区分在微信开放平台里面下载商户证书,用apiclient_cert.pem取获取'商户证书的序列号'证书查看  2.需要下载一个jar,生成微信证书时候用Releases·wechatpay-apiv3/CertificateDownloader·GitHub  3......
  • PayPal支付配置
    配置PayPal支付需要用到的是企业认证帐号 点击访问【PayPal】官网 登录帐号后,在右上角“齿轮图标”处,选择【AccountSettings】  选择左侧【Websitepayments】,找到【APIaccess】点击【Update】向下滚动页面,找到【NVP/SOAPAPIintegration(Classic)】,点击【M......