样例过不去的蒟蒻求助
查看原帖
样例过不去的蒟蒻求助
171487
cmll02楼主2020/5/3 11:04

RT

#include <stdio.h>
inline int read()
{
	int num=0,f=1;char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
	return num*f;
}
inline int min(int a,int b)
{
	return a<b?a:b;
}
struct Edge
{
    int v,w,next;
}edge[100005];
int h[1005],n,cnt=1;
inline void addedge(int u,int v,int w){
    edge[cnt].v=v,edge[cnt].w=w;edge[cnt].next=h[u];
	h[u]=cnt++;
}
#include <queue>
#include <string.h>
#include <stdlib.h>
int dis[105],inq[105],fuhuan[105];
void spfa(int s){
    std::queue<int> q;
    memset(dis,0x3f,sizeof(dis));
    memset(fuhuan,0,sizeof(fuhuan));
    dis[s]=0;
    q.push(s),inq[s]=1;fuhuan[s]=1;
    while(!q.empty()){
        int t=q.front();q.pop();
        inq[t]=0;
        for(int i=h[t];i;i=edge[i].next)
		{            
            if(dis[t]+edge[i].w<dis[edge[i].v]){
                dis[edge[i].v]=dis[t]+edge[i].w;
                if(!inq[edge[i].v])
				{
                    q.push(edge[i].v);
					inq[edge[i].v]=1;
                    if(++fuhuan[edge[i].v]>n)
					{
                        puts("-1");
                        exit(0);
                    }
                }
            }
        }
    }
}
int main()
{
	int ml,md,x,y;
	n=read(),ml=read(),md=read();
    while(ml--)addedge(read(),read(),read());
    while(md--)x=read(),y=read(),addedge(y,x,-read());
    for(int i=1;i<=n;i++)addedge(0,i,0);
    spfa(0),spfa(1);
    if(dis[n] == 0x3f3f3f3f)printf("-2\n");
    else printf("%d\n",dis[n]);
    return 0;
}
2020/5/3 11:04
加载中...