stl(图论鬼畜题)

1 篇文章 0 订阅

论stl的妙用

#include<bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define ull unsigned long long
using namespace std;
const int mod =1e4+7;
const ll INF=1e18;
typedef pair<int ,int > P;
const int maxn=6e5+5;
int n,m,k;
struct edge{
    int to,cost;
    edge(){}
    edge(int to,int cost):to(to),cost(cost){
    }
};
vector<edge>G[maxn];
int d[maxn];
P p[maxn];//point x,y
map <P,int > mp;//离散化 
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dijkstra(int s)
{
    priority_queue<P,vector<P>,greater<P> >q;
    fill(d,d+maxn,inf);
    d[s]=0;
    q.push(P(0,s));
    while(!q.empty())
    {
        P p=q.top();
        q.pop();
        int v=p.second;
        if(d[v]<p.first) continue;
        for(int i=0;i<G[v].size();i++)
        {
            edge e=G[v][i];
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                q.push(P(d[e.to],e.to));
            }
        }
    }
}
int main()
{
    cin>>n>>m>>k;
    int dd=1e4+5 ;
    mp.clear();
    for(int i=1;i<=k;i++)
    {
        int r,c;
        scanf("%d%d",&r,&c);
        //i->r(v=1) 横向纵向边改dd 
        G[i].push_back(edge(r+dd,1));
        G[i].push_back(edge(c+dd*2,1));
        G[i].push_back(edge(r+1+dd,1));
        G[i].push_back(edge(r-1+dd,1));
        G[i].push_back(edge(c+dd*2+1,1));
        G[i].push_back(edge(c+dd*2-1,1));

        G[r+dd].push_back(edge(i,0));
        G[c+dd*2].push_back(edge(i,0));
        G[r+1+dd].push_back(edge(i,0));
        G[r-1+dd].push_back(edge(i,0));
        G[c+dd*2+1].push_back(edge(i,0));
        G[c+dd*2-1].push_back(edge(i,0));

        p[i]=P(r,c);
        mp[P(r,c)] =i;  
    }   
    //if(k.around==else k) k->else k=0
    for(int i=1;i<=k;i++)
    {
        for(int j=0;j<4;j++)
        {
            int x=p[i].first+dx[j];
            int y=p[i].second+dy[j];

            if(mp.find(P(x,y))!=mp.end())
                G[i].push_back(edge(mp[P(x,y)],0));
        }
    }
    //if(n,m)is  k ,then m,n =0
    if(mp.find(P(n,m))==mp.end()){
        G[n+dd].push_back(edge(k+1,0));
        G[m+2*dd].push_back(edge(k+1,0));
    }
    int ans;
    dijkstra(mp[P(1,1)]);
    if(mp.find(P(n,m))==mp.end()){
        ans=d[k+1];
    }
    else ans=d[mp[P(n,m)]];
    if(ans==inf)
        ans=-1;
    cout<<ans<<endl;
}

代码是抄的


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值