很好的一道题呀

思路

状态\(d(i,j)\)表示已经经过了行程单中的\(i\)个城市,目前在城市\(j\)的最小代价,直接建边跑最短路就行了

比如机票为\(ACBD\),行程单为\(CD\),那么对于\((0,A)\),连向\((1,C)\),\((1,B)\),\((2,D)\)

有两个需要注意的地方

1.起点为\((1,行程单的起点)\)

2.城市编号很大,要离散化

以下是代码,离散化用\(map\)完成

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set> using namespace std; #define ull unsigned long long
#define pii pair<int, int>
#define uint unsigned int
#define mii map<int, int>
#define lbd lower_bound
#define ubd upper_bound
#define INF 0x3f3f3f3f
#define IINF 0x3f3f3f3f3f3f3f3fLL
#define DEF 0x8f8f8f8f
#define DDEF 0x8f8f8f8f8f8f8f8fLL
#define vi vector<int>
#define ll long long
#define mp make_pair
#define pb push_back
#define re register
#define il inline #define N 10000 struct Edge {
int next, from, to, w, id;
}e[2000000]; int ticketCnt, routeCnt, nodeCnt, cityCnt;
int price[250], cities[250];
vi tickets[250];
map<pii, int> nodeId;
mii cityId;
pii originNode[N+5];
int head[N+5], eid;
int d[N+5], pre[N+5];
bool inq[N+5];
int stk[N+5], tp;
queue<int> q; void addEdge(int u, int v, int w, int id) {
e[++eid] = Edge{head[u], u, v, w, id};
head[u] = eid;
} void spfa() {
memset(d, 0x3f, sizeof d);
memset(inq, 0, sizeof inq);
memset(pre, 0, sizeof pre);
int S = nodeId[mp(1, cities[1])];
d[S] = 0;
q.push(S);
while(!q.empty()) {
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].to, w = e[i].w;
if(d[v] > d[u]+w) {
d[v] = d[u]+w;
pre[v] = i;
if(!inq[v]) inq[v] = 1, q.push(v);
}
}
}
} void mark(int u) {
if(!pre[u]) return ;
stk[++tp] = e[pre[u]].id;
mark(e[pre[u]].from);
} int main() {
int kase = 0;
while(~scanf("%d", &ticketCnt) && ticketCnt) {
++kase;
nodeCnt = cityCnt = 0;
nodeId.clear();
cityId.clear();
for(int i = 1, cnt; i <= ticketCnt; ++i) {
scanf("%d%d", &price[i], &cnt);
tickets[i].clear();
for(int j = 1, x; j <= cnt; ++j) {
scanf("%d", &x);
if(!cityId.count(x)) cityId[x] = ++cityCnt;
tickets[i].pb(cityId[x]);
}
}
scanf("%d", &routeCnt);
for(int t = 1, len; t <= routeCnt; ++t) {
memset(head, 0, sizeof head);
eid = 0;
scanf("%d", &len);
for(int c = 1; c <= len; ++c) {
scanf("%d", &cities[c]);
if(!cityId.count(cities[c])) cityId[cities[c]] = ++cityCnt;
cities[c] = cityId[cities[c]];
}
for(int ticket = 1; ticket <= ticketCnt; ++ticket) {
for(int i = cities[1] == tickets[ticket][0]; i <= len; ++i) {
int cnt = i;
pii cur = mp(i, tickets[ticket][0]);
if(!nodeId.count(cur)) nodeId[cur] = ++nodeCnt, originNode[nodeCnt] = cur;
for(int j = 1; j < tickets[ticket].size(); ++j) {
if(cnt+1 <= len && cities[cnt+1] == tickets[ticket][j]) cnt++;
pii newState = mp(cnt, tickets[ticket][j]);
if(!nodeId.count(newState)) nodeId[newState] = ++nodeCnt, originNode[nodeCnt] = newState;
addEdge(nodeId[cur], nodeId[newState], price[ticket], ticket);
}
}
}
spfa();
printf("Case %d, Trip %d: Cost = %d\n", kase, t, d[nodeId[mp(len, cities[len])]]);
printf(" Tickets used: ");
tp = 0;
mark(nodeId[mp(len, cities[len])]);
for(int i = tp; i > 1; --i) printf("%d ", stk[i]);
printf("%d\n", stk[1]);
}
}
return 0;
}

UVa1048 Low Cost Air Travel——最短路的更多相关文章

  1. SCU 4444: Travel(最短路)

    Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...

  2. Travel(最短路)

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...

  3. [USACO09JAN]安全出行Safe Travel 最短路,并查集

    题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...

  4. L147 Low Cost Study Has High Impact Results For Premature Babies

    No one knows exactly why some babies are born prematurely(早产), but some of the smallest premature ba ...

  5. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集

    [BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...

  6. BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)

    题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...

  7. Minimum Transport Cost Floyd 输出最短路

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

  8. uva 1048 最短路的建图 (巧,精品)

    大白书 P341这题说的是给了NT种飞机票,给了价钱和整个途径,给了nI条要旅游的路线.使用飞机票都必须从头第一站开始坐,可以再这个路径上的任何一点下飞机一但下飞机了就不能再上飞机,只能重新买票,对于 ...

  9. 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4568 题目大意: 给一个矩阵 n*m (n m<=200),方格里如果是0~9表示通过它时要花 ...

随机推荐

  1. matplotlib画图总结--多子图布局

    1.subplot布局 subplot(nrows, ncols, index, **kwargs) subplot(pos, **kwargs) subplot(ax) x=[1,2,3] valu ...

  2. Java集合框架——Map接口

    第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...

  3. 使用mybatis出现异常:invalid comparison: java.time.LocalDateTime and java.lang.String

    整了半天终于找到问题所在:在mapper文件中,对该参数进行了和字符串的对比,如下: <if test="startTime != null and startTime != '' a ...

  4. Scala当中parallelize并行化的用法

    [学习笔记] parallelize并行化集合是根据一个已经存在的Scala集合创建的RDD对象.集合的里面的元素将会被拷贝进入新创建出的一个可被并行操作的分布式数据集.例如:val rdd03 = ...

  5. flink两种安装方式

    Flink Standalone 集群 HA 配置 1. HA 集群环境规划 使用三台节点实现两主两从集群(由于笔记本性能限制,不能开启太多虚拟机,其实使用三 台和四台机器在安装配置上没有本质区别) ...

  6. 【AtCoder】M-SOLUTIONS Programming Contest

    M-SOLUTIONS Programming Contest A - Sum of Interior Angles #include <bits/stdc++.h> #define fi ...

  7. 19牛客暑期多校 round2 H 01矩阵内第二大矩形

    题目传送门//res tp nowcoder 目的 给定n*m 01矩阵,求矩阵内第二大矩形 分析 O(nm)预处理01矩阵为n个直方图,问题转换为求n个直方图中的第二大矩形.单调栈计算,同时维护前二 ...

  8. 2019HDU暑期多校训练-1004equation-方程求解

    Description You are given two integers N,C and two integer sequences a and b of length N. The sequen ...

  9. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

  10. Python面向对象中的继承、多态和封装

    Python面向对象中的继承.多态和封装 一.面向对象的三大特性 封装:把很多数据封装到⼀个对象中,把固定功能的代码封装到⼀个代码块, 函数,对象, 打包成模块. 这都属于封装思想. 继承:⼦类可以⾃ ...