ccf 行车路线 201712-4

 90荤,不知道还有10分去哪了,未解之谜

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1002
long long  dist[maxn],sum[maxn];

struct Edge{
    int e,w,t;
};
struct Node
{
    int e;
    int w;
    friend bool operator < (Node A, Node B)
    {
        return  A.w > B.w;
    }
};

bool vis[maxn];

int m, n;
vector< vector<Edge> > G;

long long  Dij(int Star,int End)
{
    Node P, Pn;
    P.e = Star;
    P.w = 0;
    sum[0]=sum[1]=0;
    priority_queue<Node> Q;
    dist[0]=dist[1]=0;
    int i;
    for(i=2;i<=n;i++)
        dist[i]=INF;
    Q.push(P);
    while( !Q.empty() )
    {
        P = Q.top();
        Q.pop();

        if( vis[P.e] )
            continue;

        vis[P.e] = true;

        if( P.e == End )
            return P.w;
        int len = G[P.e].size();

        for(int i=0; i< len; i++)
        {
            long long cost;
            if(G[P.e][i].t)
            {
                long long temp=sum[P.e]+G[P.e][i].w;
                cost=dist[P.e]-sum[P.e]*sum[P.e]+temp*temp;
            }
            else {
                sum[G[P.e][i].e]=0;
                cost=dist[P.e]+G[P.e][i].w;
            }
            if(cost<dist[G[P.e][i].e]) {
                dist[G[P.e][i].e]=cost;
                if(G[P.e][i].t) sum[G[P.e][i].e]=sum[P.e]+G[P.e][i].w;
            }

            Pn.e = G[P.e][i].e;
            Pn.w = dist[Pn.e];
            if( !vis[Pn.e] )
                Q.push(Pn);
        }
    }
    return -1;
}

int main()
{
    Edge P;
    cin >> n >> m;
        G.clear();
        G.resize(n+1);

        memset(vis,false,sizeof(vis));

        for(int i=0; i<m; i++)
        {
            int a, b, c,k;
            cin >> k>>a >> b >> c;
            P.e = b;
            P.w = c;
            P.t=k;
            G[a].push_back(P);
            P.e = a;
            G[b].push_back(P);
        }

        long long  ans = Dij(1,n);

        cout << ans << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值