10801 - Lift Hopping//Bellman-Ford
2013-05-16 20:34
369 查看
题目描述:就是裸的最短路。但是在换乘电梯的时候要耗时60s,同时从i到j层可以有不同的地奥体达到。
题目分析:就是在建图的时候注意一点,在Bellman-Ford的过程中改一下。
下面是代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF = 1000000000;
const int maxnn = 6;
const int maxnk = 105;
int G[maxnk][maxnk];
int t[maxnn];
int temp[maxnk];
int d[maxnk];
bool inq[maxnk];
queue<int> q;
int n,k;
void Bellman_Ford()
{
for(int i = 0; i <= maxnk; i++) d[i] = INF,inq[i] = false;
d[0] = 0;
q.push(0);
inq[0] = true;
while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for(int v = 0; v < 100; v++)
{
if(d[v] > d[u] + G[u][v] + 60)
{
d[v] = d[u] + G[u][v] + 60;
if(!inq[v])
{
inq[v] = true;
q.push(v);
}
}
}
}
if(d[k] == INF) printf("IMPOSSIBLE\n");
else if(k == 0) printf("0\n");
else printf("%d\n",d[k] - 60);
}
int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
for(int i = 0; i < maxnk; i++)
{
for(int j = 0; j < maxnk; j++) G[i][j] = G[j][i] = INF;
}
for(int i = 0; i < n; i++) scanf("%d",&t[i]);
int tcount = 0;
for(int i = 0; i < n; i++)
{
int count = 0;
while(true)
{
scanf("%d",&temp[count++]);
char ch = getchar();
if(ch == '\n') break;
}
for(int i = 0; i < count; i++)
{
int u = temp[i];
for(int j = i + 1; j < count; j++)
{
int v = temp[j];
int w = (v - u) * t[tcount];
if(w < G[u][v]) G[u][v] = G[v][u] = w;
}
}
tcount++;
}
Bellman_Ford();
}
return 0;
}
题目分析:就是在建图的时候注意一点,在Bellman-Ford的过程中改一下。
下面是代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF = 1000000000;
const int maxnn = 6;
const int maxnk = 105;
int G[maxnk][maxnk];
int t[maxnn];
int temp[maxnk];
int d[maxnk];
bool inq[maxnk];
queue<int> q;
int n,k;
void Bellman_Ford()
{
for(int i = 0; i <= maxnk; i++) d[i] = INF,inq[i] = false;
d[0] = 0;
q.push(0);
inq[0] = true;
while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for(int v = 0; v < 100; v++)
{
if(d[v] > d[u] + G[u][v] + 60)
{
d[v] = d[u] + G[u][v] + 60;
if(!inq[v])
{
inq[v] = true;
q.push(v);
}
}
}
}
if(d[k] == INF) printf("IMPOSSIBLE\n");
else if(k == 0) printf("0\n");
else printf("%d\n",d[k] - 60);
}
int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
for(int i = 0; i < maxnk; i++)
{
for(int j = 0; j < maxnk; j++) G[i][j] = G[j][i] = INF;
}
for(int i = 0; i < n; i++) scanf("%d",&t[i]);
int tcount = 0;
for(int i = 0; i < n; i++)
{
int count = 0;
while(true)
{
scanf("%d",&temp[count++]);
char ch = getchar();
if(ch == '\n') break;
}
for(int i = 0; i < count; i++)
{
int u = temp[i];
for(int j = i + 1; j < count; j++)
{
int v = temp[j];
int w = (v - u) * t[tcount];
if(w < G[u][v]) G[u][v] = G[v][u] = w;
}
}
tcount++;
}
Bellman_Ford();
}
return 0;
}
相关文章推荐
- UVa 10801 - Lift Hopping
- Lift Hopping, UVa 10801【Floyd】
- Lift Hopping UVA - 10801
- uva 10801 Lift Hopping
- UVA 10801 Lift Hopping Floyd
- 习题11-7 UVa 10801 Lift Hopping SPFA最短路
- UVA 10801 Lift Hopping
- Lift Hopping - UVa 10801 Floyd
- UVa 10801 Lift Hopping / floyd
- UVa 10801 Lift Hopping / floyd
- Bellman-Ford 算法及其优化(转) 收藏
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- poj 3259 Wormholes【Bellman-Ford Vs SPFA】
- poj 2240 Arbitrage 是否存在套率 Bellman-ford
- 最短路之bellman-ford
- 算法导论 ch24 单源最短路径 Bellman-Ford
- 四、最短路径之Bellman-Ford与SPFA
- poj-3259 bellman-ford
- Bellman-Ford 算法及其优化
- bellman_ford极其优化(转)