csp201712-4 行车路线

#include<iostream>
using namespace std;
#include<queue>
#define LL long long
#define maxn 510
const int inf=1e9+7
const int maxm=1e5+10;
int n,m,arr[maxn],cnt,vis[maxn];
LL d[maxn],s[maxn];

struct edge
{
    int v,val,type,next;
}e[maxm*4];

struct node
{
    int v;
    LL dis,pre;
    node(int a,LL b,LL c)
    {
        v=a;dis=b;pre=c;
    }
    bool operator<(const node& p)const
    {
        if(dis==p.dis)
            return pre>p.pre;
        return dis>p.dis;       
    }
};
void init()
{
    cnt=0;
    for(int i=1;i<2*n+1;i++)
    {
        arr[i]=-1;
        d[i]=inf;
        if(i<=n)s[i]=0;
        else s[i]=inf;
        vis[i]=0;
    }
}
void add(int u,int v,int type,int val)
{
    e[cnt].val=val;
    e[cnt].v=v;
    e[cnt].type=type;
    e[cnt].next=arr[u];
    arr[u]=cnt++;
}
LL dj(int in,int t)
{
    priority_queue<node> q;
    d[in]=s[in]=0;
    q.push(node(in,d[in],s[in]));
    d[in+n]=s[in+n]=0;
    q.push(node(in+n,d[in+n],s[in+n]));
    while(!q.empty())
    {
        node tmp=q.top();
        q.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=arr[u];i+1;i=e[i].next)
        {
            int v=e[i].v;
            if(vis[v])continue;
            if(e[i].type)
            {//small road
                LL len=d[u]+2*(LL)e[i].val*s[u]+(LL)e[i].val*(LL)e[i].val;
                if(d[v]>len||(d[v]==len&&s[v]>s[u]+e[i].val))
                {
                    d[v]=len;
                    s[v]=s[u]+e[i].val;
                    q.push(node(v,d[v],s[v]));
                }

            }
            else
            {//big road
                if(d[v]>d[u]+e[i].val)
                {
                    d[v]=d[u]+e[i].val;
                    q.push(node(v,d[v],s[v]));
                }
            }
        }
    }
    return min(d[t],d[n+t]);
}

int main()
{
    cin>>n>>m;
    init();
    while(m--)
    {
        int t,a,b,c;
        scanf("%d%d%d%d",&t,&a,&b,&c);
        add(a,b+n*t,t,c); //a,b,0,c     a,b+n,1,c
        add(a+n,b+n*t,t,c);//a+n,b,0,c  a+n,b+n,1,c 
        add(b,a+n*t,t,c);//b,a,0,c      b,a+n,1,c
        add(b+n,a+n*t,t,c);//b+n,a,0,c  b+n,a+n,1,c
    }
    printf("%lld\n",dj(1,n));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值