牛路线Cow Routing(洛谷_3115)

好吧,这就是传说中的双关键值最短路。

说起建图来,也挺好想的嘛,反正公交车线路,爱从哪站上车,哪站下车,随你。

于是就线路上的点,两两连边。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
inline int read(){
    int t=1,num=0;char c=getchar();
    while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();}
    return num*t;
}
const int N=1010,inf=1e9;
const long long INF=1e16;
int s,t,n,a[110],g[N][N];
long long d1[N],cost[N][N];
int d2[N],lon[N][N];
void bfs(){
    for(int i=1;i<=1010;i++)
        d1[i]=INF,d2[i]=inf;
    queue<int> q;
    q.push(s);d1[s]=d2[s]=0;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=1;i<=1000;i++){
            if(!g[x][i])continue;
            int y=i,c=cost[x][y],l=lon[x][y];
            if(d1[x]+c<d1[y]){
                d1[y]=d1[x]+c;
                d2[y]=d2[x]+l;
                q.push(y);
            }
            if(d1[x]+c==d1[y]&&d2[x]+l<d2[y])
                d2[y]=d2[x]+l;
        }
    }
    if(d1[t]==INF)puts("-1 -1");
    else printf("%lld %d",d1[t],d2[t]);
}
int main()
{
    s=read();t=read();n=read();
    for(int i=1;i<=1000;i++)
        for(int j=1;j<=1000;j++)
            cost[i][j]=INF,lon[i][j]=inf;
    for(int i=1;i<=n;i++){
        long long m;scanf("%lld",&m);
        int len=read();
        for(int j=1;j<=len;j++)a[j]=read();
        for(int j=1;j<len;j++)
            for(int k=j+1;k<=len;k++){
                if(a[j]==a[k])continue;
                g[a[j]][a[k]]=1;
                if(cost[a[j]][a[k]]>m){
                    cost[a[j]][a[k]]=m;
                    lon[a[j]][a[k]]=k-j;
                }
                if(cost[a[j]][a[k]]==m&&lon[a[j]][a[k]]>k-j)
                    lon[a[j]][a[k]]=k-j;
            }
    }
    bfs();
    return 0;
}

本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。

时间: 2024-08-29 06:13:15

牛路线Cow Routing(洛谷_3115)的相关文章

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads co

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at

洛谷P2879 [USACO07JAN]区间统计Tallest Cow

To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with th

【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线

P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另外一个公交站往往要换几次车,例如从公交站A到公交站D,你就至少需要换3次车. Tiger的方向感极其糟糕,我们知道从公交站A到公交E只需要换4次车就可以到达,可是tiger却总共换了n次车,注意tiger一旦到达公交站E,他不会愚蠢到再去换车.现在希望你计算一下tiger有多少种可能的乘车方案. 题

洛谷P1027 Car的旅行路线

洛谷P1027 Car的旅行路线 题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I个城市中高速铁路了的单位里程价格为Ti,任意两个不同城市的机场之间均有航线,所有航线单位里程的价格均为t. 图例(从上而下) 机场 高速铁路 飞机航线 注意:图中并没有 标出所有的铁路与航线. 那么Car应如何安排到城市B的路线才能尽可能的节省花费呢?她发现这并不是一个简单的问题,于是她来

洛谷P1565 牛宫

洛谷P1565 牛宫 题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会算吧?).而且,AP 希望他的宫殿尽量大,能够容纳更 多的人来膜拜他.请问AP 的宫殿最后会有多大? 输入输出格式 输入格式: 第一行为N 和M.之后N 行,每行M 个数,描述的空地的海拔. 输出格式: 输出一行,表示宫殿最大面积. 输入输出样例 输入样例: 3 24 0-10

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little ene