当前位置:网站首页>2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
2022-06-27 16:30:00 【Zhong Zhongzhong】
https://www.luogu.com.cn/problem/P1038
wa I got a point , At this point c[i]<=0 when , It cannot be transferred to the next layer
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,p,c[maxn],u,cnt,out[maxn],in[maxn],head[maxn];
bool vis[maxn],flag;
queue<int>q;
struct node
{
int to,dis,nxt;
}e[maxn];
void add(int from,int to,int w)
{
e[++cnt].to=to;
e[cnt].dis=w;
e[cnt].nxt=head[from];
head[from]=cnt;
}
int main()
{
scanf("%d%d",&n,&p);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&c[i],&u);
if(c[i]>0)
q.push(i);
else
c[i]-=u;
}
for(int i=1;i<=p;i++)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
out[u]++;
in[v]++;
}
while(!q.empty())
{
int u=q.front();q.pop();
if(c[u]<=0)
continue;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].to;
c[v]+=e[i].dis*c[u];
in[v]--;
if(!in[v])
q.push(v);
}
}
for(int i=1;i<=n;i++)
{
if(!out[i]&&c[i]>0)
{
printf("%d %d\n",i,c[i]);
flag=1;
}
}
if(!flag)
cout<<"NULL"<<endl;
return 0;
}
The general method is queu Is to find the smallest dictionary order to meet the requirements of the topic topological sequence
This question adopts priority queue , Reverse construction , Construct a reverse queue with the largest lexicographic order .
Because the question requires you to put the small number forward as far as possible , So the answer is not necessarily the smallest dictionary order . however , Put the small number forward as far as possible , Such a large number will naturally go as far as possible , thus , If the topological order is reversed , Then the inverse topological order with the largest lexicographic order must be the optimal solution . therefore , We can build a backgraph to find the topological order with the largest dictionary order , Finally, output in reverse order . ( quote )
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,m,p,c[maxn],u,cnt,in[maxn],head[maxn],ans[maxn],g;
bool vis[maxn],flag;
priority_queue<int>q;
struct node
{
int to,nxt;
}e[maxn];
void add(int from,int to)
{
e[++cnt].to=to;
e[cnt].nxt=head[from];
head[from]=cnt;
}
void clean()
{
memset(head,0,sizeof(head));
memset(in,0,sizeof(in));
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
cnt=0;g=0;
while(!q.empty()) q.pop();
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
clean();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v;scanf("%d%d",&u,&v);
add(v,u);in[u]++;
}
for(int i=1;i<=n;i++)
if(!in[i]) q.push(i);
while(!q.empty())
{
int u=q.top();q.pop();
ans[++g]=u;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].to;
in[v]--;
if(!in[v]) q.push(v);
}
}
if(g<n)
printf("Impossible!\n");
else{
for(int i=n;i>=1;i--)
{
printf("%d ",ans[i]);
}
cout<<endl;
}
}
return 0;
}
P6145 [USACO20FEB]Timeline G
https://www.luogu.com.cn/problem/P6145
Adopt queue optimization , Read in as 0 Put the points in the queue
Approval code :a[v]=max(a[v],a[u]+e[i].dis); This node was first created in a[v] Days to complete , Or the precursor node + The number of days specified in the question
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
struct node
{
int to,dis,nxt;
}e[maxn];
int a[maxn],head[maxn],in[maxn],cnt,n,m,c;
void add(int from,int to,int w)
{
e[++cnt].to=to;
e[cnt].dis=w;
e[cnt].nxt=head[from];
head[from]=cnt;
}
queue<int>q;
int main()
{
scanf("%d%d%d",&n,&m,&c);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=c;i++)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);in[v]++;
}
for(int i=1;i<=n;i++)
if(!in[i])
q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].to;
a[v]=max(a[v],a[u]+e[i].dis);
in[v]--;
if(!in[v])
q.push(v);
}
}
for(int i=1;i<=n;i++)
cout<<a[i]<<endl;
return 0;
}
https://www.luogu.com.cn/record/69439301
#include <bits/stdc++.h>
#define y1 y11
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f/2;
int n,mp[maxn][maxn],x1,y1,x2,y2,ans=inf;
int dx[4]={
1,0,-1,0};
int dy[4]={
0,-1,0,1};
bool flag;
void dfs(int x,int y,int dir,int k)
{
if(k>=ans)
return ;
if(x==x2&&y==y2)
{
ans=min(ans,k);flag=1;return ;
}
for(int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(xx<=0||xx>n||yy<=0||yy>n)
continue;
if(!mp[xx][yy])
{
mp[xx][yy]=-1;
int d=0;
if(dir!=i) d=1; // In different directions , It means you will turn , This cycle 0,1,2,3 All represent one direction
if(dir==-1) d=0; // Initial value of processing direction
dfs(xx,yy,i,k+d);
mp[xx][yy]=0;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
char c;
cin>>c;
if(c=='A') x1=i,y1=j,mp[i][j]=1;
else if(c=='B') x2=i,y2=j,mp[i][j]=0;
else if(c=='x') mp[i][j]=-1;
}
}
dfs(x1,y1,-1,0);
if(flag)
cout<<ans<<endl;
else
cout<<-1<<endl;
return 0;
}
A very good search topic .
https://www.luogu.com.cn/problem/P2049
#include <bits/stdc++.h>
using namespace std;
int m,n,k,mp[105][105],ans;
bool vis[105],used[105][105][105];
int dx[2]={
1,0};
int dy[2]={
0,1};
struct node
{
int x,y,val;
};
queue<node>q;
void bfs()
{
node cur,nxt;
q.push({
1,1,mp[1][1]});
while(!q.empty())
{
cur=q.front();q.pop();
int x=cur.x,y=cur.y,w=cur.val;
if(x==m&&y==n)
{
vis[w]=1;
continue;
}
for(int i=0;i<2;i++)
{
int nx=x+dx[i],ny=y+dy[i];
int nw=(mp[nx][ny]*w)%k;
if(nx<=0||nx>m||ny<=0||ny>n||used[nx][ny][nw])
continue;
q.push({
nx,ny,nw});
used[nx][ny][nw]=1;
}
}
for(int i=0;i<k;i++)
{
if(vis[i]) ans++;
}
cout<<ans<<endl;
for(int i=0;i<k;i++)
{
if(vis[i]) cout<<i<<" ";
}
cout<<endl;
}
int main()
{
scanf("%d%d%d",&m,&n,&k);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]),
mp[i][j]%=k;
}
bfs();
return 0;
}
边栏推荐
- 2022年中国音频市场年度综合分析
- Relation and operation of ORM table
- Scrapy framework (I): basic use
- Leetcode daily practice (longest substring without repeated characters)
- 域名绑定动态IP最佳实践
- 如果想用dms来处理数据库权限问题,想问下账号只能用阿里云的ram账号吗(阿里云的rds)
- The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
- NFT dual currency pledge liquidity mining DAPP contract customization
- logstash排除特定文件或文件夹不采集上报日志数据
- 面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
猜你喜欢

# Cesium实现卫星在轨绕行

防火墙基础之源NAT地址转换和服务器映射web页面配置

The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
![[pyGame games] this](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)

The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor

Redis系列2:数据持久化提高可用性

Li Chuang EDA learning notes 16: array copy and array distribution

Bit. Store: long bear market, stable stacking products may become the main theme

Etcd可视化工具:Kstone部署(一),基于Helm快速部署

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
随机推荐
防火墙基础之源NAT地址转换和服务器映射web页面配置
What should the ultimate LAN transmission experience be like
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
实现简单的三D立方体自动旋转
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
Kubernetes基础自学系列 | Ingress API讲解
LeetCode每日一练(主要元素)
Open source 23 things shardingsphere and database mesh have to say
About MySQL: the phenomenon and background of the problem
Redis系列2:数据持久化提高可用性
The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
泰山OFFICE技术讲座:第一难点是竖向定位
如果想用dms来处理数据库权限问题,想问下账号只能用阿里云的ram账号吗(阿里云的rds)
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
基于 Nebula Graph 构建百亿关系知识图谱实践
开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
What is RPC