P4878 [USACO05DEC]Layout G

P4878 [USACO05DEC]Layout G

Title:

Insert picture description here

answer:

This question is actually a bare question with a difference constraint, but there are a few pitfalls to pay attention to
. 1. As the title says, the cows are arranged according to the number 1...N. For ML inputs, A<B
, so the relationship is B-A <10, and It's not that A-B <10, I looked it upside down at first. . The result is always wa
2. The title does not say that the graph is connected, so either run spfa for each point, or build a super source point. The distance from 0,0 to other n points is 0, and run from 0, so that all points are in one. In the connected block
3, the title requires three situations, the first is that there is no solution, the second is that there is a legal solution, but the distance is infinite, and the third is that there is a legal solution, and it is not infinite. Therefore, one spfa is not enough, because the first case is to judge the negative ring, and the priority of the negative ring is the highest, so first run spfa(0), then judge the situation 2,3, and then run spfa(1)

Code:

The last point of the code is wa I don't know why. . .
I didn't find the error in the matchmaking, crying,
my code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
    
    
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
   return s*w;
}
const int maxn=4e4+9;
struct node{
    
    
	int v,w;
};
int n,ml,md;
vector<node>edge[maxn];
int dis[maxn],vis[maxn];
int cnt[maxn];
void spfa(int now)
{
    
    
	for(int i=1;i<=n;i++)dis[i]=1e9+7;
	memset(vis,0,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	vis[now]=1;
	queue<int>q;
	dis[now]=0;
	q.push(now);
	while(!q.empty())
	{
    
    
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=0;i<edge[u].size();i++)
		{
    
    
			int v=edge[u][i].v;
			int w=edge[u][i].w;
			if(dis[v]>dis[u]+w)
			{
    
    
				dis[v]=dis[u]+w;
				cnt[v]=cnt[u]+1;
				if(cnt[v]>=n+1)
				{
    
    
					puts("-1");
					exit(0);
					//return 1;//存在负环 
				}
				if(vis[v]==0)
				{
    
    
					q.push(v);
					vis[v]=1;
				} 
			}
		}
	}
	//return 0;
}
int main()
{
    
    
	
	cin>>n>>ml>>md;
	int a,b,d;
	for(int i=1;i<=n;i++)
	{
    
    
		edge[0].push_back(node{
    
    i,0});
	}
	for(int i=1;i<=ml;i++)
	{
    
    
		cin>>a>>b>>d;
		edge[a].push_back(node{
    
    b,d});
	}
	for(int i=1;i<=md;i++){
    
    
		cin>>a>>b>>d;
		edge[b].push_back(node{
    
    a,-d});
	}
	spfa(0);
	spfa(1);
		if(dis[n]==1e9+7)cout<<-2<<endl;
		else cout<<dis[n]<<endl;
	return 0;
}
/*
4 2 1
1 3 10
2 4 20
2 3 3
*/


Correct code:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define main mian
using namespace std;
const int N=1005;
const int M=40005;
int n,ml,md,a,b,d;
struct EDGE
{
    
    
	int nxt,to,wei,;
}edge[M];
int head[N],tot;
inline void add(int x,int y,int v)
{
    
    
	edge[++tot].nxt=head[x];
	edge[tot].to=y;
	edge[tot].wei=v;
	head[x]=tot;
}
queue<int> q;
int vis[N],dis[N],circle[N];
void spfa(int s)
{
    
    
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(circle,0,sizeof(circle));
	q.push(s);
	vis[s]=1,dis[s]=0;
	while(!q.empty())
	{
    
    
		int now=q.front(); q.pop(); vis[now]=0;
		for(int i=head[now];i;i=edge[i].nxt)
		{
    
    
			int tt=edge[i].to;
			if(dis[now]+edge[i].wei<dis[tt])
			{
    
    
				dis[tt]=dis[now]+edge[i].wei;
				circle[tt]=circle[now]+1;
				if(circle[tt]>=n)
				{
    
    
					puts("-1");exit(0);
				}
				if(!vis[tt])
				{
    
    
					vis[tt]=1;
					q.push(tt);
				}
			}
		}
	}
	
}
int main()
{
    
    
	int n;
	scanf("%d%d%d",&n,&ml,&md);
	for(int i=1;i<=n;i++) add(0,i,0);
	for(int i=1;i<=ml;i++)
	{
    
    
		scanf("%d%d%d",a,b,d);
		add(a,b,d);
	}
	for(int i=1;i<=md;i++)
	{
    
    
		scanf("%d%d%d",a,b,d);
		add(b,a,-d);
	}
	spfa(0);
	spfa(1);
	if(dis[n]==INF) puts("-2");
	else printf("%d",dis[n]);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/115219658