题意:题面挺清楚啦,就是求一个最短路。只不过每个点之间的边有可能是不同线路的,要从一个线路换到另一个线路是需要花费时间的。
边有特殊的定义,那么就不以点为分析对象做最短路了。直接拿边,dis(i)表示从1到达第i条边的指向点为终点的最短距离,每次松弛找到的边的目的点是t的时候更新一下结果。
注意在出队的时候才标记vis,因为这样才能保证之前某个点在每次松弛的时候都会入队,否则便一直WA。。。。。。
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007
const long long inf = 0x3f3f3f3f3f3f3f3f;
struct Edge
{
int a,b;
long long c,t;
}edge[maxn];
struct node
{
int pos;
long long val;
bool operator<(const node &a)const
{
return val > a.val;
}
};
int n,m;
long long dis[maxn];
int vis[maxn];
vector<int> mp[maxn];
int main()
{
int t,C = 1;
//scanf("%d",&t);
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = 0; i <= n; i++)
mp[i].clear();
priority_queue<node> q;
memset(vis,0,sizeof(vis));
for(int i = 0; i < m; i++)
dis[i] = 1e18;
for(int i = 0; i < m; i++)
{
scanf("%d%d%lld%lld",&edge[i].a,&edge[i].b,&edge[i].c,&edge[i].t);
if(min(edge[i].a,edge[i].b) == 1)
{
dis[i] = edge[i].t;
q.push(node{i,dis[i]});
}
mp[edge[i].a].push_back(i);
mp[edge[i].b].push_back(i);
}
while(!q.empty())
{
node st = q.top();
q.pop();
int x = st.pos;
if(vis[x])
continue;
vis[x] = 1;
int a = edge[x].a;
int b = edge[x].b;
int len1 = mp[a].size();
int len2 = mp[b].size();
for(int i = 0; i < len1; i++)
{
int y = mp[a][i];
long long val = edge[y].c - edge[x].c;
if(val < 0)
val = -val;
if(dis[y] > dis[x] + val + edge[y].t)
{
dis[y] = dis[x] + val + edge[y].t;
q.push(node{y,dis[y]});
}
}
for(int i = 0; i < len2; i++)
{
int y = mp[b][i];
long long val = edge[y].c - edge[x].c;
if(val < 0)
val = -val;
if(dis[y] > dis[x] + val + edge[y].t)
{
dis[y] = dis[x] + val + edge[y].t;
q.push(node{y,dis[y]});
}
}
}
long long ans = 1e18;
for(int i = 0; i < mp[n].size(); i++)
ans = min(ans,dis[mp[n][i]]);
printf("%lld\n",ans);
}
return 0;
}