J drizzle subway (hierarchical shortest)

Links: https://ac.nowcoder.com/acm/contest/949/J
Source: Cattle-off network

City where a total of rain m subway lines, respectively designated Line 1, Line 2, ......, m Line. The city a total of n stations, numbered 1 ~ n  . Wherein the line number i takes a sitting A i price per one stop need to spend more bi price. Line i have C i  stations, and that C i  stations are known, if a subway line through a plurality of stations, it can take the subway line to another in the station, and can transfer a plurality of times . Now I want to drizzle from subway stations to the s t stations, subway waiting time is negligible, find the least cost price, and if it can not reach the output -1 . (The subway is two-way, so s may be greater than t )

Enter a description:

The first line of the input four positive integers n- , m , S , T respectively represent the number of stations, subway line number, starting point and the terminal. 
The second row to m + . 1 line, the number of each row of three A i , B i , C i , i represent take Line price, number of lines per i spend one stop price, i Line The number of stations. Next, C i number, i represents the number of each station line numbers monotonically.

Output Description:

Total row number indicates a minimum cost, if it can not reach the output of -1 .
Example 1

Entry

5 2 1 4
2 2 3 1 3 5
2 1 4 2 3 4 5

Export

7

Explanation

Take the Line 1: In 2; 

1 . 3 : In 2;

Transfer Line 2: take 2;

. 3 . 4 : In 1;

therefore the minimum total cost of 7.

Remarks:

. 1 n- 10 00 , . 1 m 500 , . 1 S , T n- 

. 1 A I , B I 100 , . 1 C I n- , m [Sigma I = . 1 C I 10 . 5

ideas: Per underground lines can be seen in FIG layer, between a normal built layer by layer edge to FIG. It may be connected between each layer, how to deal with?
Corresponding to each station to establish a virtual point. As long as each station corresponding to the virtual connection point. Is a virtual connection point is connected to the station to spend money on the subway (metro or transfer money).
The station is connected to the virtual point takes 0, the effect achieved in transit.


Benefits of established virtual point: not according to the attached edge between layers, so each must be connected, it is necessary m * m * n edges, as long as the established virtual point n * m * 2.
Calculation is also convenient, but the shortest path from s to find virtual point t is the result of the virtual point. No virtual point, correspond to each station to each subway station s t metro for the most short-circuited.

Code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=2100;
struct node{
    int u,v,w,next;
}e[maxn*maxn]; 
int head[maxn*maxn],n,m,s,t,cnt;
int a[maxn],b[maxn],d[maxn*maxn],vis[maxn*maxn];
void add(int u,int v,int w)
{
    e[cnt]={u,v,w,head[u]};
    head[u]=cnt++;
}

void spfa(int st)//spfa
{
    d[st]=0;
    queue<int> q;
    q.push(st);
    vis[st]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            int w=e[i].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        } 
    }        
}

int main()
{
    int num,x,last;
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(d,0x3f,sizeof(d));
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&num);
        for(int j=1;j<=num;j++)
        {
            scanf("D% " , & X);
             IF (! J = . 1 ) 
            { 
                the Add ((I - . 1 ) * + n-X, (I- . 1 ) * n-+ Last, B [I]); // same line station is connected to 
                the Add ((I- . 1 ) * n-+ Last, (I- . 1 ) * + n- X, B [I]); 
            } 
            the Add ((I - . 1 ) * + n-X, n-m + X *, 0 ); // associated with each virtual point 
            the Add (n-m * + X, (I- . 1 ) * + n- X, a [I]); 
            Last = X; // previous recording stop 
        } 
    } 
    SPFA (n-*m+s);//最短路 
    if(d[n*m+t]<inf)
        printf("%d\n",d[n*m+t]);
    else
        printf("-1\n");
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/xiongtao/p/11183255.html