poj2502 Subway(建图)

题意:给你家和学校的坐标,然后在给你若干车站的坐标,人步行的速度是10km/h,火车的速度40km/h。问你从家到学校至少需要多少分钟。

思路:思路很好想,一看就是一个最短路的模板题。但是这个建图真是恶心死我了。前几天做了好几遍这个题目,一直结果不对,今天结果终于大差不差,然后又忘记不同铁轨之间可以步行建图。。。可以加一个vector数组用来保存已经出现的车站,没加入一个点就要与所有出现的点建图(步行),车站只有相邻才可以按火车速度建图。

AC Code:

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<sstream>
#include<algorithm>

using namespace std;
#define read(x) scanf("%d",&x)
#define Read(x,y) scanf("%d%d",&x,&y)
#define sRead(x,y,z)  scanf("%d%d%d",&x,&y,&z)
#define gc(x)  scanf(" %c",&x);
#define mmt(x,y)  memset(x,y,sizeof x)
#define write(x) printf("%d\n",x)
#define INF 0x3f3f3f3f
#define ll long long
#define mod  998244353
#define pdd pair<double,double>
const int N = 1000;
const int M=  1e6;
struct Edge
{
    int next;
    int to;
    double dis;
}edge[M*2];
int head[N],tot;
inline void add(int from,int to,double dis)
{
    edge[++tot].next = head[from];
    edge[tot].to = to;
    edge[tot].dis = dis;
    head[from] = tot;
}
double cal(pdd a,pdd b)//两点间的距离
{
    double M = (a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second);
    M = sqrt(M);
    return M;
}
int cnt = 1;
map<pdd,int> hap;
int id(pdd a){//返回节点编号
    if(hap.count( a)) return hap[a];
    else return hap[a] = cnt ++;
}
double d[1005];bool vis[1005];
void spfa(int n)
{
    mmt(vis,0);
    for(int i = 1;i <= n;++i) d[i] = 1e10;
    queue<int> Q;
    Q.push(1);
    d[1] = 0;
    vis[1] = 1;
    while(Q.size()){
        int x = Q.front();
        Q.pop();
        vis[x] = 0;
        for(int i = head[x];~i;i = edge[i].next){
            int y = edge[i].to;
            double dis = edge[i].dis;
            if(d[y] >d[x] +dis){
                d[y] = d[x] +dis;
                if(!vis[y]){
                    vis[y] = 1;
                    Q.push(y);
                }
            }
        }
    }
}
vector<pdd> st;
int main()
{
    //freopen("input.txt","r",stdin);
    mmt(head,-1);
    pdd S,T;
    cin>>S.first>>S.second>>T.first>>T.second;
    int u = id(S),v = id(T);
    if(u == v) return cout<<0,0;
    add(u,v,cal(S,T)*3/500);
    add(v,u,cal(T,S)*3/500);
    pdd p,k,m;
    st.push_back(S);
    st.push_back(T);
    while(cin>>p.first>>p.second&&p.first!=-1){
        for(int i = 0;i < st.size();++i){
            int f = id(p),t = id(st[i]);
            add(f,t,cal(st[i],p)*3/500);
            add(t,f,cal(st[i],p)*3/500);
        }
        st.push_back(p);
        while(cin>>k.first>>k.second&&k.first!=-1){
            for(int i = 0;i < st.size();++i){
            int f = id(k),t = id(st[i]);
            add(f,t,cal(st[i],k)*3/500);
            add(t,f,cal(st[i],k)*3/500);
        }
            int f = id(p),t = id(k);
            add(f,t,cal(p,k)*3/2000);
            add(t,f,cal(p,k)*3/2000);
            st.push_back(k);
            p = k;
        }
    }
    spfa(cnt);
    cout<<fixed<<setprecision(0)<<d[2]<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值