您的位置:首页 > 其它

Bus System

2015-08-03 13:38 295 查看
[align=left]Problem Description[/align]
Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it’s still playing an important role even now.

The bus system of City X is quite strange. Unlike other city’s system, the cost of ticket is calculated based on the distance between the two stations. Here is a list which describes the relationship between the distance and the cost.



Your neighbor is a person who is a really miser. He asked you to help him to calculate the minimum cost between the two stations he listed. Can you solve this problem for him?

To simplify this problem, you can assume that all the stations are located on a straight line. We use x-coordinates to describe the stations’ positions.

 

[align=left]Input[/align]
The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.

Each case contains eight integers on the first line, which are L1, L2, L3, L4, C1, C2, C3, C4, each number is non-negative and not larger than 1,000,000,000. You can also assume that L1<=L2<=L3<=L4.

Two integers, n and m, are given next, representing the number of the stations and questions. Each of the n
4000
ext n lines contains one integer, representing the x-coordinate of the ith station. Each of the next m lines contains two integers, representing the start
point and the destination.

In all of the questions, the start point will be different from the destination.

For each case,2<=N<=100,0<=M<=500, each x-coordinate is between -1,000,000,000 and 1,000,000,000, and no two x-coordinates will have the same value.

 

[align=left]Output[/align]
For each question, if the two stations are attainable, print the minimum cost between them. Otherwise, print “Station X and station Y are not attainable.” Use the format in the sample.

 

[align=left]Sample Input[/align]

2
1 2 3 4 1 3 5 7
4 2
1
2
3
4
1 4
4 1
1 2 3 4 1 3 5 7
4 1
1
2
3
10
1 4

 

[align=left]Sample Output[/align]

Case 1:
The minimum cost between station 1 and station 4 is 3.
The minimum cost between station 4 and station 1 is 3.
Case 2:
Station 1 and station 4 are not attainable.

题解:将条件转化成两点之间的花费就是求最短路径了。

迪杰斯特拉算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>

using namespace std;

const long long INF = 1e18;

long long map[103][103];
long long d[103];
bool visited[103];
long long l[5];
long long c[5];
int pos[103];

void djistra(int n,int x)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = map[x][i];
}
d[x] = 0;
visited[x] = true;

for(int i = 1;i < n;i++)
{
long long min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > d[j])
{
min = d[j];
k = j;
}
}
if(min == INF)
{
break;
}
visited[k] = true;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && d[j] > d[k] + map[k][j])
{
d[j] = d[k] + map[k][j];
}
}
}
}

int main()
{
int T;
cin>>T;
int tt = 1;
while(T--)
{
for(int i = 1;i < 5;i++)
{
scanf("%d",&l[i]);
}
for(int i = 1;i < 5;i++)
{
scanf("%d",&c[i]);
}

int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",&pos[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}

for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
long long t = abs(pos[j] - pos[i]);
if(t > 0 && t <= l[1])
{
map[i][j] = c[1];
}
else if(t > l[1] && t <= l[2])
{
map[i][j] = c[2];
}
else if(t > l[2] && t <= l[3])
{
map[i][j] = c[3];
}
else if(t > l[3] && t <= l[4])
{
map[i][j] = c[4];
}
else if(t > l[4])
{
map[i][j] = INF;
}
}
}

printf("Case %d:\n",tt++);
while(m--)
{
int s,e;
scanf("%d%d",&s,&e);
djistra(n,s);
if(d[e] == INF)
{
printf("Station %d and station %d are not attainable.\n",s,e);
}
else
{
printf("The minimum cost between station %d and station %d is %lld.\n",s,e,d[e]);
}
}
}

return 0;
}


堆优化的迪杰斯特拉:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>

using namespace std;

const long long INF = 1e18;

long long map[103][103];
long long d[103];
bool visited[103];
long long l[5];
long long c[5];
int pos[103];

struct Node
{
int to;
long long cost;
Node(int a,long long b)
{
to = a;
cost = b;
}
bool operator< (Node t) const
{
return cost > t.cost;
}
};

void djistra(int n,int x)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[x] = 0;
priority_queue<Node> q;
q.push(Node(x,0));
while(!q.empty())
{
Node p = q.top();
q.pop();
if(!visited[p.to])
{
visited[p.to] = true;
for(int i = 1;i <= n;i++)
{
if(!visited[i] && d[i] > d[p.to] + map[p.to][i])
{
d[i] = d[p.to] + map[p.to][i];
q.push(Node(i,d[i]));
}
}
}
}
}

int main()
{
int T;
cin>>T;
int tt = 1;
while(T--)
{
for(int i = 1;i < 5;i++)
{
scanf("%d",&l[i]);
}
for(int i = 1;i < 5;i++)
{
scanf("%d",&c[i]);
}

int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",&pos[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}

for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
long long t = abs(pos[j] - pos[i]);
if(t > 0 && t <= l[1])
{
map[i][j] = c[1];
}
else if(t > l[1] && t <= l[2])
{
map[i][j] = c[2];
}
else if(t > l[2] && t <= l[3])
{
map[i][j] = c[3];
}
else if(t > l[3] && t <= l[4])
{
map[i][j] = c[4];
}
else if(t > l[4])
{
map[i][j] = INF;
}
}
}

printf("Case %d:\n",tt++);
while(m--)
{
int s,e;
scanf("%d%d",&s,&e);
djistra(n,s);
if(d[e] == INF)
{
printf("Station %d and station %d are not attainable.\n",s,e);
}
else
{
printf("The minimum cost between station %d and station %d is %lld.\n",s,e,d[e]);
}
}
}

return 0;
}


SPFA:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>

using namespace std;

const long long INF = 1e18;

long long map[103][103];
long long d[103];
bool visited[103];
long long l[5];
long long c[5];
int pos[103];

void spfa(int n,int x)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = INF;
}
d[x] = 0;
queue<int> q;
q.push(x);
visited[x] = true;

while(!q.empty())
{
int p = q.front();
q.pop();
visited[p] = false;
for(int i = 1;i <= n;i++)
{
if(d[i] > d[p] + map[p][i])
{
d[i] = d[p] + map[p][i];
if(!visited[i])
{
q.push(i);
visited[i] = true;
}
}
}
}
}

int main()
{
int T;
cin>>T;
int tt = 1;
while(T--)
{
for(int i = 1;i < 5;i++)
{
scanf("%d",&l[i]);
}
for(int i = 1;i < 5;i++)
{
scanf("%d",&c[i]);
}

int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",&pos[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}

for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
long long t = abs(pos[j] - pos[i]);
if(t > 0 && t <= l[1])
{
map[i][j] = c[1];
}
else if(t > l[1] && t <= l[2])
{
map[i][j] = c[2];
}
else if(t > l[2] && t <= l[3])
{
map[i][j] = c[3];
}
else if(t > l[3] && t <= l[4])
{
map[i][j] = c[4];
}
else if(t > l[4])
{
map[i][j] = INF;
}
}
}

printf("Case %d:\n",tt++);
while(m--)
{
int s,e;
scanf("%d%d",&s,&e);
spfa(n,s);
if(d[e] == INF)
{
printf("Station %d and station %d are not attainable.\n",s,e);
}
else
{
printf("The minimum cost between station %d and station %d is %lld.\n",s,e,d[e]);
}
}
}

return 0;
}


弗洛伊德(别人过了,我的超时):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>

using namespace std;

const long long INF = 1e18;

long long map[103][103];
long long d[103];
bool visited[103];
long long l[5];
long long c[5];
int pos[103];

long long min(long long a,long long b)
{
return a > b ? b : a;
}

void floyd(int n)
{
for(int k = 1;k <= n;k++)
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
map[i][j] = min(map[i][j],map[i][k] + map[k][j]);
}
}
}
}

int main()
{
int T;
cin>>T;
int tt = 1;
while(T--)
{
for(int i = 1;i < 5;i++)
{
scanf("%d",&l[i]);
}
for(int i = 1;i < 5;i++)
{
scanf("%d",&c[i]);
}

int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
scanf("%d",&pos[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}

for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
long long t = abs(pos[j] - pos[i]);
if(t > 0 && t <= l[1])
{
map[i][j] = c[1];
}
else if(t > l[1] && t <= l[2])
{
map[i][j] = c[2];
}
else if(t > l[2] && t <= l[3])
{
map[i][j] = c[3];
}
else if(t > l[3] && t <= l[4])
{
map[i][j] = c[4];
}
else if(t > l[4])
{
map[i][j] = INF;
}
}
}

printf("Case %d:\n",tt++);
while(m--)
{
int s,e;
scanf("%d%d",&s,&e);
floyd(n);
if(map[s][e] == INF)
{
printf("Station %d and station %d are not attainable.\n",s,e);
}
else
{
printf("The minimum cost between station %d and station %d is %lld.\n",s,e,map[s][e]);
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: