HNCPC 2016 湖南省程序设计竞赛 F - 地铁 dijkstra变形

题目链接

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808

题意

n个地铁站,m条地铁线路,每条线路之间有一个运行时间,线路c1转线路c2需要额外花费|c1-c2|的时间,求从1到n的最少时间

思路

每个点,从每个线路过来都视作一个状态,然后跑dijkstra。。一开始我认为c为1e5数组开不下,没想到可以用map这个黑科技 (╥╯^╰╥)

#include<cstdio>
#include<queue>
#include<cstring>
#include<map>
#include<iostream>
using namespace std;
const int maxm = 1e5+10;
const int maxn = 1e5+10;
const int INF = 2e9+2;
struct edge
{
    int v,c,w,next;
}e[maxm*2];
struct node
{
    int to,c,d;
    bool operator < (const node &b)const
    {
        return d>b.d;
    }
};
int head[maxm];
int tot;
int abs(int a)
{
    if(a<0)return -a;
    else return a;
}
map<int,int> mp[maxn];
map<int,int> vis[maxn];
map<int,int>::iterator it;
void addedge(int u,int v,int c,int t)
{
    e[tot].v=v;
    e[tot].w=t;
    e[tot].c=c;
    e[tot].next=head[u];
    head[u]=tot++;

    e[tot].v=u;
    e[tot].w=t;
    e[tot].c=c;
    e[tot].next=head[v];
    head[v]=tot++;
}
int dijstra(int s,int t,int n)
{
    priority_queue<node> q;
    for(it=mp[s].begin();it!=mp[s].end();it++)
    q.push(node{s,it->first,0});
    while(!q.empty())
    {
        node a=q.top();q.pop();
        int u=a.to,lc=a.c;
        if(u==t)return a.d;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            int w=e[i].w;
            int c=e[i].c;
            if(mp[v][c]>w+a.d+abs(c-lc))
            {
                mp[v][c]=w+a.d+abs(c-lc);
                q.push(node{v,c,mp[v][c]});
            }
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        tot=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<m;i++)
        {
            int u,v,c,t;
            scanf("%d%d%d%d",&u,&v,&c,&t);
            addedge(u,v,c,t);
            mp[u][c]=INF;
            mp[v][c]=INF;
        }
        int ans=dijstra(1,n,n);
        for(int i=0;i<=n;i++)
            mp[i].clear();
        cout<<ans<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值