1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; struct HeapNode { int d,u; bool operator < (const HeapNode& b) const { return d>b.d; } }; struct Edge { int from,to,mod,type; }; vector<Edge>edges[maxn]; int n,m,s,e,vst[maxn],dist[maxn],val[55][2005],tmp[55]; void AddEdge(int x,int y,int c,int t) { edges[x].push_back((Edge) { x,y,c,t }); } void Dijkstra() { priority_queue<HeapNode>Q; for(int i=1; i<=n; i++)dist[i]=0x7fffffff/2; memset(vst,0,sizeof(vst)); dist[1]=s; Q.push((HeapNode) { s,1 }); while(!Q.empty()) { int Now=Q.top().u; Q.pop(); if(vst[Now])continue; vst[Now]=1; for(Edge& e:edges[Now]) { int Next=e.to; int Dist=val[e.type][dist[Now]%e.mod]; if(dist[Next]>dist[Now]+Dist) { dist[Next]=dist[Now]+Dist; Q.push((HeapNode) { dist[Next],Next }); } } } } int main() { n=Get_Int(); m=Get_Int(); s=Get_Int(); e=Get_Int(); for(int i=1; i<=m; i++) { int a=Get_Int(),b=Get_Int(),c=Get_Int(),d=Get_Int(); tmp[i]=c; for(int j=0; j<c; j++)val[i][j]=(a*j+b)%c; for(int t=1; t<=2; t++) { val[i][c]=val[i][0]; for(int j=c; j>=1; j--)val[i][j-1]=min(val[i][j-1],val[i][j]+1); } for(int j=0; j<c; j++)val[i][j]+=d; } for(int i=1; i<=e; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); AddEdge(x,y,tmp[v],v); } Dijkstra(); for(int i=2; i<=n; i++) if(dist[i]==0x7fffffff/2)puts("-1"); else printf("%d\n",dist[i]-s); return 0; }
|