P7831 [CCO2021] Travelling Merchant
妙妙题。
题意
给定 -1
。
分析
不难发现原题等价于最后在环上转圈。
设
但是这个转移有一个漏洞:这个转移是会成环的。那么复杂度就可能会退化成平方暴力。
考虑另一种思路:
首先,无法到达一个环上的点肯定没用。判断方法可以建反边然后跑拓扑,没有拓扑序的点就是有用的点。删掉无用点之后,不难发现该图所有点都可以通过它的任意一条出边到达一个环。
考虑取出
删掉一条边之后,若该点没有出度可以更新了,那么当前的
整个操作,我们需要对
代码丑陋,谨慎观看。
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0)
#define OTIE cout.tie(0)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
struct EDGE{
int u,v,w,p;
bool ban;
bool operator<(const EDGE &pp)const{return w>pp.w;}
}e[maxn];
//struct edge{
// int to,nxt,w,p;
//}a[maxn];
//int head[maxn],edges;
//void add(int x,int y,int z,int w){
// a[++edges]=(edge){y,head[x],z,w};
// head[x]=edges;
//}
int out[maxn];
int f[maxn];
vector<pii>G[maxn];
queue<int>q;
void init(){
rep(i,1,n)if(!out[i])q.push(i);
while(!q.empty()){
int nw=q.front();q.pop();
for(pii qwq:G[nw]){
int u=qwq.fi,id=qwq.se;
e[id].ban=1;
if(!(--out[u]))q.push(u);
}
}
}
inline void solve_the_problem(){
n=rd(),m=rd();
rep(i,1,m){
int x=rd(),y=rd(),z=rd(),w=rd();
e[i].u=x,e[i].v=y,e[i].w=z,e[i].p=w;
}
sort(e+1,e+m+1);
rep(i,1,m){
out[e[i].u]++,G[e[i].v].emplace_back(mp(e[i].u,i));
}
init();
// rep(i,1,n)write(out[i],32);P__;
// rep(i,1,m)write(e[i].ban,32);P__;
mem(f,0x3f);
rep(i,1,m)if(!e[i].ban){
f[e[i].u]=min(f[e[i].u],e[i].w),--out[e[i].u],e[i].ban=1;
if(!out[e[i].u])q.push(e[i].u);
while(!q.empty()){
int nw=q.front();q.pop();
for(pii qwq:G[nw]){
int u=qwq.fi,id=qwq.se;
if(!e[id].ban){
// printf("edge %d->%d,value=%d,f[nw]=%d,e[id].w=%d\n",u,nw,max(e[id].w,f[nw]-e[id].p),f[nw],e[id].w);
f[u]=min(f[u],max(e[id].w,f[nw]-e[id].p)),e[id].ban=1;
if(!(--out[u]))q.push(u);
}
}
}
}
rep(i,1,n)write(f[i]==inf?-1:f[i],32);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现