URAL 1205 By the Underground or by Foot?(SPFA)

By the Underground or by Foot?

Time limit: 1.0 second
Memory limit: 64 MB

Imagine yourself in a big city. You want to get from point A to point B.
To do that you may move by foot or use the underground. Moving by the
underground is faster but you may enter and exit it only at the
stations. To save your time you decided to write a program to find the
fastest route.

Input

The
first line contains two floating point numbers. First of them is the
speed of traveling by foot. The second one is the speed of traveling by
the underground. The second speed is always greater than the first one.

Then description of the underground follows. It starts with an integer number N in the first line. It is the number of the underground stations. You may assume that N is not greater than 200. The following N lines contain two floating point numbers each (i-th line contains the coordinates of i-th
station). Then the description of the connections between stations
follows. Each connection is determined by the pair of integers, i.e. by
the numbers of connected stations. The list of connections is terminates
with a pair of zeroes. We assume that all the connections are straight.
So the time we need to travel between stations is equal to the distance
between stations divided by the speed of traveling by the underground.

It
should be mentioned also that entering and exiting the underground and
changing trains are possible at the stations only and takes no time.

At last the coordinates of the points A and B are given, tha pair of coordinates in a line.

Output

The first line should contain the minimal time needed to get from the point A to the point B. Time should be given with the precision of 10−6.
The second line describes the use of the underground while traveling.
It starts with the number of visited stations with tha following list of
visited stations in the order they should be visited.

Sample

input output
1 100
4
0 0
1 0
9 0
9 9
1 2
1 3
2 4
0 0
10 10
10 0
2.6346295
4 4 2 1 3

Problem Author: Alexander Klepinin

【分析】最短路,SPFA。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=205;
const int M=100005;
int n,m,k=0;
double a,b;
int vis[N],pre[N];
double w[N][N],d[N];
char str[20];
bool flag=false;
struct man{
    double x,y;
}point[N];
void spfa()
{
    d[0]=0;
    vis[0]=1;
    queue<int>q;
    q.push(0);
    while(!q.empty()){
        int t=q.front();q.pop();
        vis[t]=0;
        for(int i=0;i<=n+1;i++){
            if(d[i]>d[t]+w[t][i]){
                d[i]=d[t]+w[t][i];
                pre[i]=t;
                if(!vis[i])q.push(i),vis[i]=1;
            }
        }
    }
}
int main() {
    for(int i=0;i<N;i++){
        d[i]=10000000;
        for(int j=0;j<N;j++){
            w[i][j]=10000000;
        }
    }
    scanf("%lf%lf",&a,&b);
    scanf("%d",&n);
    int u,v;

    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&point[i].x,&point[i].y);
    }
    while(~scanf("%d%d",&u,&v)&&u&&v){
        double s=sqrt((point[u].x-point[v].x)*(point[u].x-point[v].x)+(point[u].y-point[v].y)*(point[u].y-point[v].y));
        w[u][v]=w[v][u]=s/b;
    }
    scanf("%lf%lf%lf%lf",&point[0].x,&point[0].y,&point[n+1].x,&point[n+1].y);
    for(int i=0;i<=n+1;i++){
        for(int j=i+1;j<=n+1;j++){
            if(w[i][j]<10000000)continue;
            double s=sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y));
            w[i][j]=w[i][j]=s/a;
        }
    }
    spfa();
    printf("%.7lf\n",d[n+1]);
    stack<int>p;
    for(int i=n+1;pre[i];i=pre[i]){
        p.push(pre[i]);
    }
    printf("%d",p.size());
    while(!p.empty())printf(" %d",p.top()),p.pop();
    return 0;
}

时间: 2024-10-06 14:29:35

URAL 1205 By the Underground or by Foot?(SPFA)的相关文章

Ural 1353 Milliard Vasya&#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j],表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说,总可以看成在前i-1位后面加上一个0~9,所以状态转移方程就很容易出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <

URAL 1099 Work scheduling 一般图的最大匹配 带花树算法(模板)

R - Work scheduling Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1099 Description There is certain amount of night guards that are available to protect the local junkyard from possible junk r

URAL 1104 Don’t Ask Woman about Her Age(数论)

题目链接 题意 : 给你一个数,未知进制,然后让你从2到36进制中找出一个最小的进制K,满足给你的这个数作为k进制时能够整除k-1. 思路 : 有一个公式,(a*b^n)mod(b-1)=a: 给定你这个数,当做字符串输入的时候,这个数转化成10进制的结果应该是:a[0]*k^(n-1)+a[1]*k^(n-2)+……+a[n-1]*k^0,然后题目要求的就是这个式子的结果取余(k-1)为0, 经过最开始给出的公式,将该式子化简得(a[0]+a[1]+……+a[n-1])%(k-1),所以只要满

URAL 1167. Bicolored Horses (DP)

题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所有的马都必须入马厩.有两种颜色的马,如果 i 匹黑马与 j 匹白马同在一个马厩,不愉快系数是 i * j,总系数就是k个系数相加.让总系数最小. 思路 : dp[i][j] 代表的是前 i 个马厩放 j 匹马的最小不愉快系数值. 1 //1167 2 #include <cstdio> 3 #in

ural 1932 The Secret of Identifier (容斥原理)

题目大意: 求出给的n个串中. 精确到只有一个字符不同,两个字符不同,三个字符不同,四个字符不同的对数. 思路分析: 枚举状态. dp[i] [j] ...表示当前串取出 i 状态下的所有字符转化成十进制数为 j 的出现的次数. 这样的话,就记录了所有串的子串的状态. 然后计数就得到了所有的状态. 然后我们要得到精确不同的,可以用补集的思想,如果要精确到三个不相同,意味着要精确到1 个是相同的. 注意的问题是 在最后要运用容斥去重. #include <cstdio> #include <

Ural 1260 A nudnik photographer(DP)

A nudnik photographer 大意: 对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法. 思路: 对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1]:当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3]:还有最后一种情况是1357--8642. 所以DP方程为dp[i] = dp[i-1]+dp[i-3]+1. #include <stdio.h> int n; int dp[100]; int

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

Ural 1353 Milliard Vasya&amp;#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j].表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说.总能够看成在前i-1位后面加上一个0~9.所以状态转移方程就非常easy出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计就可以. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #inclu