Niuke.com-2018 Xiangtan University Programming Contest-F

Topic link: https://www.nowcoder.com/acm/contest/105/F

Problem-solving ideas: The first direct idea of ​​this question is to search, but after thinking about it for a long time, I didn't think there is any good way to search, and then I turned it into the shortest way to write,

Because there are multiple entrances and exits, a sink point and a source point are established, the weights are set by themselves, and then the weights that can be connected up, down, left, and right are 1, and the weights of the entrance and exit points of the transmission matrix (if it can be used) are set to 3;

Then there is the shortest path;

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 400005
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
    int next;
    int to;
    int w;
}edge[maxn];
struct node
{
    int num;
    int dist;
    node(int _num=0,int _dist=0):num(_num),dist(_dist){}
    friend bool operator<(node a,node b)
    {
        return a.dist>b.dist;
    }
};
int head[maxn];
int cnt;
int dist[maxn];
int visit[maxn];
int ans[maxn];
void add(int u,int v,int w)
{
    edge[cnt].next=head[u];
    edge[cnt].to=v;
    edge[cnt].w=w;
    head[u]=cnt++;
}
void dij (int u)
{
    priority_queue<node>q;
    memset(dist,inf,sizeof(dist));
    memset(visit,0,sizeof(visit));
    dist[u]=0;
    q.push(node(u,0));
    while(!q.empty())
    {
        node p=q.top();
        q.pop();
        int now=p.num;
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            Edge e = edge [i];
            if(dist[e.to]>dist[now]+e.w)
            {
                dist[e.to]=dist[now]+e.w;
                q.push(node(e.to,dist[e.to]));
            }
        }
    }
}
intmain()
{
    int n,m,q;
    int x,y,w;
    int tx, ty, ex, ey;
    int cot=0;
    char s[500][500];
    while(cin>>n>>m>>q)
    {
        cot=cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>s[i][j];
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            if(s[i][j]!='#')
            {
                if(s[i+1][j]!='#'&&i+1<=n)
                {
                    x=(i-1)*m+j;y=(i)*m+j;add(x,y,1);
                }
                if(s[i-1][j]!='#'&&i-1>=1)
                {
                    x=(i-1)*m+j;y=(i-2)*m+j;add(x,y,1);
                }
                if(s[i][j+1]!='#'&&(j+1)<=m)
                {
                    x=(i-1)*m+j;y=(i-1)*m+j+1;add(x,y,1);
                }
                if(s[i][j-1]!='#'&&(j-1)>=1)
                {
                    x=(i-1)*m+j;y=(i-1)*m+j-1;add(x,y,1);
                }
            }
            if(s[i][j]=='S')
            {
                x=0;y=(i-1)*m+j;add(x,y,1);
            }
            if(s[i][j]=='T')
            {
                ans[++cot]=(i-1)*m+j;
            }
        }
        while(q--)
        {
            cin>>tx>>ty>>ex>>ey;
            tx ++; ty ++; ex ++; ey ++;
            if(s[tx][ty]!='#'&&s[ex][ey]!='#')
            {
            x = (tx-1) * m + ty; y = (ex-1) * m + ey;
            add(x,y,3);
            }
        }
        dij (0);
        int a=inf;
        for(int i=1;i<=cot;i++)
        {
            a=min(a,dist[ans[i]]);
        }
        if(a==inf)
        cout<<"-1\n";
        else
        cout<<a-1<<endl;
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292601&siteId=291194637