Noip analog Day6.13 by LD T1

Source: Internet
Author: User
Tags cmath

First, Zie Zhe home

"Title description":

Zie Zhe School, ready to take the bus home. Because anxious to go home to see the MSN Warrior, so he wants to use the shortest time to go home, but because the school is far away from home, sometimes he needs to turn a few cars to go home. Waiting for a while, and the speed of each bus is not the same, Zie Zhe dizzy, do not know how to be in the shortest possible time to go home, as a cjoier, you should help him.

Now has been Zie Zhe in the city has N bus station (school in a site, Zie Zhe home in the N site) and M bus lines, each bus line is an orderly collection, bus line I contains Pi site A[I,1],A[I,2]...A[I,PI], indicating that I bus from a[i,1] departure, After A[i,2],a[i,3]...a[i,pi] and back to a[i,1].

Ti,ri the time of the ranking I bus and the time of bus No. I after one station (if you are currently on the bus line of section I, you can spend TI's time waiting for this car, then the car will spend RI time passing one stop, the drop off time is negligible).

"Input":

The first row of two number n,m, indicating the number of bus stations and bus lines.

The following m lines, the first three digits of each line of the i+1 line are TI,RI,PI, followed by the number of Pi, the number of j+3 a[i,j].

"Output":

Only one row, which represents the minimum time required.

"Sample Input":

7 ·

3 3 3 1 2 3

10 1 2) 1 3

1 1 2) 3 2

"Sample Output":

8

"description":

Zie Zhe from the school (1) Take bus No. 1th to station 2nd, and then from station 2nd to the No. 3rd car home.

20% of data n,m≤10;

60% of data n,m≤100;

100% of Data n,m≤500;pi≤30.

The answer of the question person:

It can be converted to the shortest-circuit model.

There are two ways to transfer this place:

1. We can then split each point into M-points, we use f[i,j] to represent from point 1th to I this point and sit on the J Road bus of the minimum cost, for the same line adjacent to two points, we even a value of the RJ edge, for not the same line of two points, even a rj+tk edge, The final answer is Min{f[n,i]} (1≤I≤M);

2. We define F[I] for the minimum cost of getting off at point I, then for the same route on the point 22 of the edge, the cost is: the number of sides *rj+tk; You can find that the pi is small, using the latter one edge way better, the number of points is N, the edge is M*P2. Specific use of SPFA implementation.

Even more trouble, I write dijkstra+heap

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <cmath>using namespace std;typedef long long LL; #define INF 1<<30#define N 100010struct edge{int v,w,next;} E[n];int head[n];int cnt;struct data{int To,len;} P;struct Cmp{bool operator () (const data A,const data b) {return a.len>b.len;}}; int n,m,dian;int t,r,pp;int turn;int vis[n],dist[n];void link (int u,int v,int W) {e[++cnt]= (edge) {v,w,head[u]};head[u]= CNT;} void Dij (int s) {priority_queue<data,vector<data>,cmp>q;for (int i=1;i<=dian;i++) Dist[i]=INF;dist[s] =0;vis[s]=1;for (int i=head[s];i;i=e[i].next) if (DIST[E[I].V]&GT;DIST[S]+E[I].W) {dist[e[i].v]=dist[s]+e[i].w;p.to =e[i].v;p.len=dist[p.to];q.push (P);} for (int i=1;i<=dian-1;i++) {if (Q.empty ()) Break;p=q.top (), Q.pop (), while (Vis[p.to] &&!q.empty ()) P=q.top ( ), Q.pop (); int x=p.to;//int len=p.len;vis[x]=1;for (int j=head[x];j;j=e[j].next) if (DIST[E[J].V]&GT;DIST[X]+E[J].W) {Dist[e[j].v]=dist[x]+e[j].w;p.to=e[j].v;p.len=dist[p.to];q.push (P);}}} int main () {freopen ("home.in", "R", stdin), Freopen ("Home.out", "w", stdout), scanf ("%d%d", &n,&m);d ian=n+1;for (int i=1;i<=m;i++) {scanf ("%d%d%d", &t,&r,&pp), for (int j=1;j<pp;j++) {scanf ("%d", &turn); link (turn,dian,t); Link ( dian,turn,0); link (dian,dian+1,r);d ian++;} Link (dian,dian+1-pp,r), scanf ("%d", &turn), Link (turn,dian,t), link (dian,turn,0);d ian++;} DIJ (1);p rintf ("%d\n", Dist[n]); return 0;}

  

Room Another god Ben wrote the SPFA

#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath > #include <algorithm> #include <ctime> #include <vector> #include <queue> #include <map  > #include <set> #ifdef WIN32 #define OT "%i64d" #else # define OT "%lld" #endifusing namespace Std;typedef long Long ll;const int MAXN = 520;const int inf = (1<<30); int n,m;int Dis[maxn][maxn];int next[maxn][maxn];int T[MAXN],R[MAXN ],p[maxn][maxn];bool pd[maxn][maxn];struct node{int x, y;}; Queue<node>q;int ans;inline int getint () {int W=0,q=0;char c=getchar (); while (c< ' 0 ' | | c> ' 9 ') && c!=  '-') C=getchar (), if (c== '-') q=1, C=getchar (); while (c>= ' 0 ' && c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q? -w:w;}    inline void Spfa () {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j++) Dis[i][j]=inf; while (!    Q.empty ()) Q.pop ();    Node Lin; for (int i=1;i<=n;i++) if (next[i][1]!=0) {lin.x=i; lin.y=1; Q.push (Lin);d is[i][1]=t[i];PD [I][1]=1;} while (! Q.empty ()) {int X=q.front (). x; int Y=q.front (). Y; Q.pop ();        pd[x][y]=0; for (int i=1;i<=n;i++) {if (i!=x) {if (next[i][y]==0) continue;if (Dis[i][y]>dis[x][y]+t[i]) {Dis[i][y]=dis[x][y] +t[i];if (!pd[i][y]) {pd[i][y]=1;lin.x=i; lin.y=y; Q.push (Lin);} }} else{if (Dis[x][next[x][y]]>dis[x][y]+r[i]) {dis[x][next[x][y]]=dis[x][y]+r[i];if (!pd[x][next[x][y]) {pd[x] [Next[x][y]]=1;lin.x=x; Lin.y=next[x][y]; Q.push (Lin);}    }}}} Ans=inf;    for (int i=1;i<=n;i++) {if (Dis[i][n]<ans) ans=dis[i][n];    }}inline void Solve () {n=getint (); M=getint (); for (int i=1;i<=m;i++) {t[i]=getint (); R[i]=getint (); P[i][0]=getint (); for (int j=1;j<=p[i][0];j++) {p[i][j]=getint (); if (j!=1) next[i][p[i][j-1]]=p[i][j];}    if (p[i][0]>0) next[i][p[i][p[i][0]]]=p[i][1];    } SPFA (); printf ("%d", ans); int main () {freopen ("home.in", "R", stdin), Freopen ("Home.out", "w", stdout); solve (); fclose (stdin); fclose (stdout); return 0;}

  

Noip analog Day6.13 by LD T1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.