您的位置:首页 > 其它

Choose the best route

2015-08-03 11:23 267 查看
[align=left]Problem Description[/align]
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s
home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
 

[align=left]Input[/align]
There are several test cases.

Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands
for the bus station that near Kiki’s friend’s home.

Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .

Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.

 

[align=left]Output[/align]
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 

[align=left]Sample Input[/align]

5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1

 

[align=left]Sample Output[/align]

1
-1

题解:求其他点到某一个点的距离只需要把边反转求该点到其他点的距离就行了。因为a->b相当于b->a,此时权值是a->b的权值,而a->b的权值是b->a以前的权值.

迪杰斯特拉:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int INF = 0x3fffffff;

int map[1003][1003];
int d[1003];
bool visited[1003];

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] = true;

for(int i = 1;i < n;i++)
{
int 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 n,m,s;
while(scanf("%d%d%d",&n,&m,&s) != EOF)
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j &l
c8b2
t;= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}
int p,q,t;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&p,&q,&t);
map[q][p] = min(map[q][p],t);
}

int w;
cin>>w;
int ans = INF;
djistra(n,s);
while(w--)
{
int st;
scanf("%d",&st);
ans = min(ans,d[st]);
}

if(ans == INF)
{
printf("-1\n");
}
else
{
printf("%d\n",ans);
}
}

return 0;
}

djistra优化:

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

using namespace std;

const int INF = 0x3fffffff;

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

//vector<Node> vec[1003];
int map[1003][1003];
int d[1003];
bool visited[1003];

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

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

int main()
{
int n,m,s;
while(scanf("%d%d%d",&n,&m,&s) != EOF)
{
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;
}
}
}
int p,q,t;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&p,&q,&t);
map[q][p] = min(map[q][p],t);
}

int w;
cin>>w;
int ans = INF;
djistra(n,s);
while(w--)
{
int st;
scanf("%d",&st);
ans = min(ans,d[st]);
}

if(ans == INF)
{
printf("-1\n");
}
else
{
printf("%d\n",ans);
}
}

return 0;
}
SPFA:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int INF = 0x3fffffff;

int map[1003][1003];
int d[1003];
bool visited[1003];

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

int main()
{
int n,m,s;
while(scanf("%d%d%d",&n,&m,&s) != EOF)
{
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;
}
}
}
int p,q,t;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&p,&q,&t);
map[q][p] = min(map[q][p],t);
}

int w;
cin>>w;
int ans = INF;
spfa(n,s);
while(w--)
{
int st;
scanf("%d",&st);
ans = min(ans,d[st]);
}

if(ans == INF)
{
printf("-1\n");
}
else
{
printf("%d\n",ans);
}
}

return 0;
}

弗洛伊德(超时):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int INF = 0x3fffffff;

int map[1003][1003];

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++)
{
if(map[i][j] < map[i][k] + map[k][j])
{
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
}

int main()
{
int n,m,s;
while(scanf("%d%d%d",&n,&m,&s) != EOF)
{
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;
}
}
}
int p,q,t;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&p,&q,&t);
map[q][p] = min(map[q][p],t);
}

int w;
cin>>w;
int ans = INF;
floyd(n);
while(w--)
{
int st;
scanf("%d",&st);
ans = min(ans,map[s][st]);
}

if(ans == INF)
{
printf("-1\n");
}
else
{
printf("%d\n",ans);
}
}

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