传送门:地铁
思路:拆点,最短路;拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化)
/************************************************************** Problem: User: youmi Language: C++ Result: Accepted Time: Memory: ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d ",a) #define ptlld(a) printf("%I64d ",a) #define rep(i,from,to) for(int i=from;i<=to;i++) #define irep(i,to,from) for(int i=to;i>=from;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define eps 1e-6 #define oo (1e16) #define TEST cout<<"*************************"<<endl const double pi=4*atan(1.0); using namespace std; typedef long long ll; template <class T> inline void read(T &n) { char c; int flag = 1; for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar()); if (c == '-') flag = -1, n = 0; else n = c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; n *= flag; } ll Pow(ll base, ll n, ll mo) { if (n == 0) return 1; if (n == 1) return base % mo; ll tmp = Pow(base, n >> 1, mo); tmp = (ll)tmp * tmp % mo; if (n & 1) tmp = (ll)tmp * base % mo; return tmp; } //*************************** int n,m; const int maxn=100000+10; const ll mod=1000000007; struct node { int v,c; ll t,dis; node(){} node(int _v,int _c,ll _t,ll _dis) { v=_v,c=_c,t=_t,dis=_dis; } bool operator<(const node &a)const { if(dis==a.dis) return t>a.t; return dis>a.dis; } }; vector<node>vt[maxn]; void dijkstra() { ll ans=oo; priority_queue<node>q; for(int i=0;i<vt[1].size();i++) { vt[1][i].dis=vt[1][i].t; if(vt[1][i].v==n) ans=min(ans,vt[1][i].dis); q.push(vt[1][i]); } while(!q.empty()) { node cur=q.top(); q.pop(); for(int i=0;i<vt[cur.v].size();i++) { node temp=vt[cur.v][i]; if(temp.dis>cur.dis+temp.t+abs(cur.c-temp.c)) { temp.dis=cur.dis+temp.t+abs(cur.c-temp.c); if(temp.v==n) ans=min(ans,temp.dis); vt[cur.v][i]=temp;//非常重要,不要忘了这一步 q.push(temp); } } } printf("%lld ",ans); } int main() { #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); #endif while(~sc2(n,m)) { int a,b,c; ll t; for(int i=1;i<=n;i++) vt[i].clear(); rep(i,1,m) { sc3(a,b,c); scanf("%lld",&t); vt[a].push_back(node(b,c,t,oo)); vt[b].push_back(node(a,c,t,oo)); } dijkstra(); } }