题目链接
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;
}
}