很好的一道题呀

思路

状态\(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. MySQL索引解析(联合索引/最左前缀/覆盖索引/索引下推)

    本节内容: 1)索引基础 2)索引类型(Hash索引.有序数组.B+树) 3)索引的几个常见问题 1)联合索引 2)最左前缀原则 3)覆盖索引 4)索引下推 1. 索引基础 索引对查询的速度有着至关重 ...

  2. oracle 的安装 及环境的配置...

    前言 最近这段时间和香港同事一起做项目  负责给日本客户做一个产品  使用的是Oracle 数据库   在一开始项目启动时一直报和oracle数据库有关    本机在这之前就已经安装好了    由于使 ...

  3. Vue.directive()的用法和实例

    官网实例: https://cn.vuejs.org/v2/api/#Vue-directive https://cn.vuejs.org/v2/guide/custom-directive.html ...

  4. Dijstra_优先队列_前向星

    Dijstra算法求最短路径 具体实现方式 设置源点,将源点从原集u{}中取出并放入新建集s{} 找出至源点最近的点q从原集取出放入新集s{} 由q点出发,更新所有由q点能到达的仍处于原集的点到源点的 ...

  5. 剑指offer42:数组和一个数字S,输出两个数的乘积最小的

    1 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. ...

  6. 10.使用du将文件按大小进行排序

    按G进行排序du -sh * | grep G | sort -nr

  7. JWT与Session比较和作用

    1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...

  8. HeidiSQL 导入Excel数据

    一 前言 原文出处:http://blog.csdn.net/qq_27727681/article/details/53944744 二 效果演示: 2000多条数据,顺利导入成功. 三  实现方法 ...

  9. MySQL SQL Training

    源于知乎:50道SQL练习题 一.表数据 1.学生表——Student ),Sname ),Sage )); ' , '赵雷' , '1990-01-01' , '男'); ' , '钱电' , '1 ...

  10. centos中拉取postgre

    新搭建好的linux服务器环境,docker也配置好了. 第一步,下载postgre docker pull postgres:11 这里的版本号自己按照自己的需要来获取. 然而实际上没那么顺利,直接 ...