码迷,mamicode.com
首页 > 其他好文 > 详细

ccf 2017 12 - 4 行车路线

时间:2018-09-20 20:22:50      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:creat   bool   map   maps   opera   friend   empty   return   ccf   

 

技术分享图片

 

 

技术分享图片

 

 

技术分享图片

附上代码:

 

#include<stdio.h>
#include<string.h>
#define inf 0x7fffffff
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXM = 1e6 + 5;
int n, m;
typedef struct NODE
{
    int to;
    ll temp;
    ll cost;
    int ff;
    friend bool operator < (const NODE &a, const NODE &b) {
        return a.cost > b.cost;
    }
}node;
typedef struct Edge
{
    int from;///起点
    ll temp;///记录权值
    int f;///标记小路
    int next;
    int to;
    ll w;
} edge;
edge maps[MAXM];
int head[505];
ll dist[505];
int vids[505];
int cnt;
void creat ()
{
    for(int i=0; i<505; i++)
        head[i]=-1;
    cnt=0;
}
void add(int t,int u,int v,ll w)
{
    maps[cnt].f = t;
    maps[cnt].from = u;
    maps[cnt].to=v;
    maps[cnt].w=w;
    maps[cnt].next=head[u];///一开始放置为-1,-1的条件就可以跳出
    ///下一步接着储存,head[u]的值,就是前面的位置
    head[u]=cnt++;///head[u]会得到这条线的值
}
ll dijkstra(int s,int t)
{
    for(int i=0; i<505; i++)
    {
        vids[i]=0;
        dist[i]=inf;
    }
    priority_queue<node>que;
    node begins;
    begins.to = s;
    begins.temp = 0;
    begins.cost = 0;
    begins.ff = 0;
    que.push(begins);
    while(!que.empty())
    {
        node ends=que.top();
        que.pop();
        if(!vids[ends.to])
        {
            vids[ends.to]=1;
            dist[ends.to]=ends.cost;
            for(int i = head[ends.to]; ~i; i = maps[i].next)
            {
                int to = maps[i].to;
                ll w = maps[i].w;
                int flag = maps[i].f;
                if(!vids[to])
                {
                    node ans;
                    ans.to=to;
                    if(ends.ff == 0 && flag == 0)
                    {
                        ans.cost = ends.cost + w;
                        ans.ff = flag;
                        ans.temp = 0;
                    }
                    else if(ends.ff == 1 && flag == 0)
                    {
                        ans.cost = ends.cost + w;
                        ans.ff = flag;
                        ans.temp = 0;
                    }
                    else if(ends.ff == 1 && flag == 1)
                    {
                        ans.cost = ends.cost - (ends.temp * ends.temp) + (ends.temp + w) * (ends.temp + w);
                        ans.ff = flag;
                        ans.temp = ends.temp + w;
                    }
                    else if(ends.ff == 0 && flag == 1)
                    {
                        ans.cost = ends.cost + (w * w);
                        ans.ff = flag;
                        ans.temp = w;
                    }
                    que.push(ans);
                }
            }
        }
    }
    if(dist[t] == inf)
    {
        return -1;
    }
    else
    {
        return dist[t];
    }
}
int main()
{
   while(~scanf("%d%d",&n, &m))
   {
       creat();
       int t, a, b;
       long long c;
       for (int i = 0; i < m; i ++) {
           scanf("%d%d%d%lld",&t, &a, &b, &c);
           add(t, a, b, c);
           add(t, b, a, c);
       }
       ll re = dijkstra(1, n);
       printf("%lld\n",re);
   }
    return 0;
}

 

ccf 2017 12 - 4 行车路线

标签:creat   bool   map   maps   opera   friend   empty   return   ccf   

原文地址:https://www.cnblogs.com/qq136155330/p/9683000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!