BZOJ 2763: [JLOI2011]飞行路线(分层图最短路)
2015-11-23 20:37
519 查看
2763: [JLOI2011]飞行路线
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1440 Solved: 548
[Submit][Status][Discuss]
Description
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?Input
数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)
Output
只有一行,包含一个整数,为最少花费。
Sample Input
5 6 10 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
8HINT
对于30%的数据,2<=n<=50,1<=m<=300,k=0;对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.
解题思路:
傻逼题,一开始用SPFA一直wa,改成Dijkstra才过。
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include <map> #define ll long long using namespace std; const int MAXN = 50000 + 10; const int INF = 0x3f3f3f3f; struct Edge { int to, next, w; }edge[MAXN<<1]; int tot, head[MAXN]; void init() { tot = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v, int w) { edge[tot].to = v; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot++; } int n, m, k, st, ed; int vis[MAXN][20], f[MAXN][20]; void spfa() { queue<pair<int, int> > q; memset(f, INF, sizeof(f)); memset(vis, 0, sizeof(vis)); for(int i=0;i<=k;i++) { f[st][i] = 0; q.push(make_pair(st, i)); vis[st][i]++; } while(!q.empty()) { pair<int, int> pii = q.front(); q.pop(); int u = pii.first, x = pii.second; vis[u][x]--; for(int i=head[u];i!=-1;i=edge[i].next) { int v = edge[i].to, w = edge[i].w; if(f[v][x] > f[u][x] + w) { f[v][x] = f[u][x] + w; if(!vis[v][x]) { q.push(make_pair(v, x)); vis[v][x]++; } } if(f[v][x + 1] > f[u][x] && x < k) { f[v][x + 1] = f[u][x]; if(!f[v][x+1]) { q.push(make_pair(v, x + 1)); vis[v][x + 1]++; } } } } int ans = 0x7fffffff; for(int i=0;i<=k;i++) ans = min(ans, f[ed][i]); printf("%d\n", ans); } int main() { init(); scanf("%d%d%d", &n, &m, &k); scanf("%d%d", &st, &ed); int u, v, w; for(int i=1;i<=m;i++) { scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); } spfa(); return 0; }
相关文章推荐
- QUT——An Easy Math Problem (三次函数求最大值)
- ubuntu apt-get 安装软件时package has no installtion candidate问题
- response.getWriter().write()与out.print()的区别(转)
- A. PawnChess
- neoclipse连接neo4j
- POJ1887 Testing the CATCHER
- linux下C++对线程的封装
- OpenCV CommandLineParser 的用法
- cmake安装遇到问题总结
- POJ 3070 Fibonacci (矩阵快速幂)
- 安全组规则与iptables规则的关系
- Oracle中创建表时Storage字段的含义
- python StringIO类
- 产看Linux运行时间
- cheng@Linux之基础--存储器管理实验
- Python猜数字游戏
- hdu1796容斥
- Count And Say
- Objective-C基础知识点总结
- jsp的九大内置对象