stl(图论鬼畜题)

来源:互联网 发布:linux copy命令不覆盖 编辑:程序博客网 时间:2024/05/22 15:11

论stl的妙用

#include<bits/stdc++.h>#define inf 0x7fffffff#define ll long long#define ull unsigned long longusing 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,ymap <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;}代码是抄的
原创粉丝点击