岛屿逃生

前言

蒟蒻原创
U63277 岛屿逃生

Description

\(n\)个岛屿和\(m\)条路径,你不知什么原因,被传送到了第\(r\)个岛屿,你必须逃离这里,有一个损坏的传送门(但是你可以修好它)在第\(c\)个岛屿,所以你的目标是到达第\(c\)个岛屿,你有一个魔法值\(t\),由于你的能力有限,所以如果你想修好传送门,你的魔法值必须为正。在第\(x_i\)个岛屿到第\(y_i\)个岛屿有一条路和一个数值\(z_i\),当你从第\(x_i\)个岛屿到第\(y_i\)个岛屿时(或者从第\(y_i\)个岛屿到第\(x_i\)个岛屿时),你的魔法值\(t\)要乘上\(z_i\)\(z_i\)有可能是一个负数,所以你的魔法值\(t\)也有可能是负数。一条路你可以重复走多次。

你现在要从第\(r\)个岛屿到第\(c\)个岛屿,因为传送门有损坏,所以当你到达第\(c\)个岛屿时,你当前的魔法值\(t\)是正数,你才能修好传送门。

现在要你求逃离这里后魔法值最小可以为多少,如无法逃离则输出\(-1\)

Input

\(1\)行,两个数,\(n\)\(m\)(\(5<=n<=3000,n<=m<=6000\)),表示有\(n\)个岛屿;
\(2\)~\(n+1\)行,每行三个数,\(x_i\)\(y_i\)\(z_i\)(\(1\)<=\(x_i\)\(y_i\)<=\(n\)\(-10\)<=\(z_i\)<=\(10\)\(z_i ≠0\)),表示第\(x_i\)个岛屿到第\(y_i\)个岛屿有一条路,这个路的数值为\(zi\)
\(n\)+\(3\)行,两个数,\(r\)\(c\),表示第\(r\)个岛屿到第\(c\)个岛屿。

Output

一个最小的正数\(t\)\(-1\)

Sample

Input

Sample Input#1

  3 3
  1 2 -1
  2 3 -2
  1 3 4
  1 3

Sample Input#2

  3 3
  1 2 -2
  2 3 2
  1 3-2
  1 3

Sample Input#3

  5 8
  2 3 4
  1 3 3
  2 4 -2
  1 4 -5
  3 5 -1
  2 5 -2
  1 5 3
  4 5 2
  1 2

Output

Sample Output#1

  4

Sample Output#2

  -1

Sample Output#3

  6

Explanation

Sample explanation#1

\(1-2-3\)

Sample explanation#2

两种路径结果是\(-4\)\(-2\),都不是正数,所以输出\(-1\)

Sample explanation#2

\(1-3-5-2\)

Train of Thought

麻烦的是高精
我们建一个\(2n\)的图,\(1\)\(n\)是正数区域,\(n+1\)\(2n\)是负数区域
SPFA时如果遇到负数,就跳区域
意思是读入后建边
如果是正数,就建负数区域互通的和正数区域互通的无向边(因为正数\(*\)正数\(=\)正数;负数\(*\)正数\(=\)负数)
如果是负数,就建正数区域通往负数区域和负数区域通往正数区域的无向边(因为正数\(*\)负数\(=\)负数;负数\(*\)负数\(=\)正数)
SPFA还是正常的SPFA
特别的是要高精加压位
还有建负数的无向边时要取绝对值,因为分了正负数区域

蒟蒻可能讲的不是很清楚,可以在评论里问我,或与我私聊(852671959)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
const ll N=4010,M=25010,W=300,Y=1e10;
struct BNN{
	ll val[W];
}f[N*2];
BNN operator *(BNN &a,ll &b)//重新定义*为高精乘
{
	BNN ans;ll g=0;
	for(ll i=0;i<W;i++)
	{
		ans.val[i]=a.val[i]*b+g;
		g=ans.val[i]/Y;
		ans.val[i]%=Y;
	}
	return ans;
}
bool operator <(BNN &a,BNN &b)//重新定义<
{
	for(ll i=W-1;i>=0;i--)
	{
		if(a.val[i]>b.val[i]) return false;
		else if(a.val[i]<b.val[i]) return true;
	}
	return false;
}
void dy(BNN &a,BNN b)//将b数组赋到a数组
{
    for(ll i=0;i<W;i++)
        a.val[i]=b.val[i];
}
struct node//邻接表
{
    ll to,next,w;
}a[M*2];
ll ls[2*N],tot,n,m,r,c;
bool v[2*N];
queue<int> q;
void addl(ll x,ll y,ll w)
{
    a[++tot].to=y;
    a[tot].next=ls[x];
    ls[x]=tot;
    a[tot].w=w;
}
void SPFA()//SPFA
{
    for(ll i=1;i<=2*n;i++)
      f[i].val[W-1]=999;
    f[r].val[W-1]=0;
    f[r].val[0]=1;
    q.push(r);
    while(!q.empty())
    {
        ll x=q.front();q.pop();
        for(ll i=ls[x];i;i=a[i].next)
        {
            ll y=a[i].to;BNN z=f[x]*a[i].w;
	            if(z<f[y]){
                dy(f[y],z);
                if(!v[y]){
                    v[y]=true;
                    q.push(y);
                }
            }
        }
        v[x]=false;
    }
}
void write(BNN as)
{
	if(as.val[W-1]==999){printf("-1");return;}//判断是否为0
	ll w=W-1;
    while(!as.val[w]) w--;
	printf("%lld",as.val[w]);
    while(w--)//压位
    {
        if(as.val[w]<1e8) putchar('0');
        if(as.val[w]<1e7) putchar('0');
        if(as.val[w]<1e6) putchar('0');
        if(as.val[w]<1e5) putchar('0');
        if(as.val[w]<1e4) putchar('0');
        if(as.val[w]<1e3) putchar('0');
        if(as.val[w]<1e2) putchar('0');
        printf("%lld",as.val[w]);
    }
}
int main()
{
	freopen("date.in","r",stdin);
	freopen("date.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=m;i++){
        ll x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        if(z>0){
            addl(x,y,z);
            addl(y,x,z);
            addl(x+n,y+n,z);
            addl(y+n,x+n,z);//赋边(正)
        }
        else{
        	z=abs(z);//见上(思路)
            addl(x,y+n,z);
            addl(y,x+n,z);
            addl(x+n,y,z);
            addl(y+n,x,z);//赋边(负)
        }
    }
    scanf("%lld%lld",&r,&c);
    SPFA();
    write(f[c]);//高精输出
    return 0;
}
posted @ 2020-08-22 19:58  20Maple  阅读(231)  评论(0)    收藏  举报
Live2D