题:https://www.luogu.org/problem/P3115

题意:给出起点A,终点B,N条路线,下面没俩行一个路线,第一行是俩个数,第一个为这条路线的花费,第二个为这条路线经过的点数n,第二行即为n个整数表示这条路径;

分析:1、题目有说如果要跳转航线就要花费被跳往航线的的费用,所以单单连一条中转的边是错的;

   2、题目范围1000,所以我们暴力建边,但也要建得有思路,对于每一条航线,如果你一直在这条航线上走,花费都是不变的(即为这条航线的cost),所以我们可以认为,对于这条航线的每一个点 i 都可以直接花费cost到 i 后面的点 j ,所以就预处理最小花费和经 过的点数,再添加图的边;

   3、最后spfa一下就好,花费为第一优先级,经过的点数为第二优先级;

#include<bits/stdc++.h>
using namespace std;
inline int read(){
int sum=,x=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')
x=;
ch=getchar();
}
while(ch>=''&&ch<='')
sum=(sum<<)+(sum<<)+(ch^),ch=getchar();
return x?sum:-sum;
}
inline void write(int x){
if(x<)
putchar('-'),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int M=1e3+;
int maxx=,tot,head[M],vis[M],a[M];
ll dis[M][],cost[M][M],path[M][M];
struct node{
int v,nextt;
ll cost,w;
}e[M*M];
void addedge(int u,int v,ll w,ll cost){
e[tot].v=v;
e[tot].w=w;
e[tot].cost=cost;
e[tot].nextt=head[u];
head[u]=tot++;
}
void spfa(int s,int t){
for(int i=;i<=;i++)
dis[i][]=INF;
queue<int>que;
que.push(s);
dis[s][]=;
while(!que.empty()){
int u=que.front();
que.pop();
vis[u]=;
for(int i=head[u];~i;i=e[i].nextt){
int v=e[i].v;
if(dis[v][]>dis[u][]+e[i].w){
dis[v][]=dis[u][]+e[i].w;
dis[v][]=dis[u][]+e[i].cost;
if(!vis[v]){
vis[v]=;
que.push(v);
}
}
else if(dis[v][]==dis[u][]+e[i].w){
if(dis[v][]>dis[u][]+e[i].cost)
dis[v][]=dis[u][]+e[i].cost;
}
}
}
if(dis[t][]==INF)
printf("-1 -1\n");
else
printf("%lld %lld\n",dis[t][],dis[t][]);
}
int main(){
int A=read(),B=read(),t=read();
memset(head,-,sizeof(head));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
cost[i][j]=inf;
maxx=;
while(t--){
int w=read(),n=read();
for(int i=;i<=n;i++)
a[i]=read(),maxx=max(maxx,a[i]);
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(cost[a[i]][a[j]]>w){
cost[a[i]][a[j]]=w;
path[a[i]][a[j]]=j-i;
}
}
for(int i=;i<=maxx;i++)
for(int j=;j<=maxx;j++)
if(cost[i][j]<inf){
addedge(i,j,cost[i][j],path[i][j]);
}
spfa(A,B);
return ;
}

Cow Routing(最短路spfa)的更多相关文章

  1. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  2. L - Subway(最短路spfa)

    L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...

  3. BZOJ4992 [Usaco2017 Feb]Why Did the Cow Cross the Road 最短路 SPFA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4992 题意概括 在一幅n*n的地图上,Amber从左上角走到右下角,每走一步需要花费时间t,每走完 ...

  4. BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

    这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...

  5. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

    求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...

  7. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  8. 【POJ】3255 Roadblocks(次短路+spfa)

    http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立 ...

  9. 【wikioi】1269 匈牙利游戏(次短路+spfa)

    http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...

随机推荐

  1. Docker安装 - CentOS7环境

    Docker安装 - CentOS7环境 安装Docker 我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核 ...

  2. Java 面向对象异常处理,finally,覆盖时异常特点,package,import,包之间的访问(10)

    Java 面向对象异常处理, finally:final 关键字的用法参考http://www.cnblogs.com/itcqx/p/5541659.html 覆盖时异常特点,package,imp ...

  3. java 连接mysql 示例

    import java.sql.*; public class Main { // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL static final String JDBC ...

  4. 从[Greenplum 6.0] 1分钟安装尝鲜开始

    Greenplum目前6版本目前已经迭代了几个小版本了,随着版本的更新,不断的有bug被修复. 打算试用的朋友可以入手了. 作为开年的第一个工作日的第一个帖子,必须从“开天辟地”的6.0开始.以下内容 ...

  5. 如何在C语言 C++里面调用 DOS命令

    C里面调用可以用[system("命令")]这样的形式. 但需要include <stdlib.h> 例子如下: #include <stdio.h> #i ...

  6. Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency

    Error : Execution failed for task ’ :app: preDebugAndroidTestBuild’.Conflict with dependency ‘com.an ...

  7. ORBSLAM2的资源

    ORBSLAM2代码总结 https://blog.csdn.net/hzwwpgmwy/article/details/82462988 ORBSLAM2局部地图更新实现 https://blog. ...

  8. 杨辉三角(C语言)

    杨辉三角 杨辉三角,是二项式系数在三角形中的一种几何排列,中国南宋数学家杨辉1261年所著的<详解九章算法>一书中出现.在欧洲,帕斯卡(1623----1662)在1654年发现这一规律, ...

  9. debian8.8安装sougou输入法

    传送门:http://www.cnblogs.com/ligongzi/p/6137601.html 亲测可用

  10. Downton Abbey

    1. 当女儿以为泰坦尼克号不会沉的时候,父亲用了一个有意思的比喻: - I thought it was supposed to be unsinkable. - Every mountain is ...