1. 程式人生 > >Codeforces Detour(Dijkstra +爆搜)

Codeforces Detour(Dijkstra +爆搜)

Description

After last year’s edition of the BAPC, you are still stuck in Delft. In order to participate again this year, you are going to Amsterdam by bus. During the journey you look out of the window and look for traffic signs that point in the direction of Amsterdam. To your surprise, you notice that the bus is never taking the roads that are pointed out by the signs! You think that the bus company might have chosen a route such that, at no intersection, the bus goes in the direction that is pointed to by the signs. Your friends, however, find this very unbelievable, and don’t think this is possible. Can you figure out whether there exists a bus route that satisfies this requirement? Note that a bus route never visits the same place twice. A traffic sign pointing in the direction of the shortest route to Amsterdam is placed at every intersection. You may assume that the input graph is both simple and connected, and that there is a unique optimal route to Amsterdam from every intersection.

Input

• A single line containing two integers: n (2 ≤ n ≤ 105 ), the number of intersections, and m (1 ≤ m ≤ 106 ), the number of undirected roads that connect the intersections. The intersections are numbered from 0 to n − 1. Delft is denoted by intersection i = 0 and Amsterdam is denoted by intersection i = 1.
• m lines that specify the roads – A road is specified by three integers, ai , bi (0 ≤ ai , bi < n and ai 6= bi) and di (0 ≤ di ≤ 500000), where ai and bi are ids of the two intersections that are connected by this road and di is the distance that the bus has to travel to get from ai to bi or vice versa.

Output

As output, give one of the following:
• A path from Delft to Amsterdam that satisfies the requirements, in case such a path exists. – A path is specified by a single line containing an integer k, the length of the path, followed by k integers pi that specify the intersections along the path in the order in which they are crossed, with p0 = 0 and pk−1 = 1.
• The text “impossible”, if such a path does not exist.

Sample

Sample Input 1 
4 5
0 2 5
2 1 5
0 3 10
3 1 20
3 2 5
Sample Output 1
3 0 3 1

Sample Input 2 
4 3
0 1 10
1 2 20
2 3 30
Sample Output 2
impossible

Sample Input 3 
7 9
0 1 800
6 2 300
2 3 75
3 4 80
4 5 50
4 1 100
1 6 35
0 2 120
0 3 100
Sample Output 3
2 0 1

題目描述

現有n個路口,每個路口的路牌都會標記到 1 路口的最短路的下一個節點,現判斷從0->1且不能走路牌上標記的方向,是否存在一種答案. 若存在輸出路徑,否則輸出”impossible”.

解題思路

標記源點為1,先用dijkstar標記每個路口的路牌上指明的節點,然後標記的路線不可行,直接dfs爆搜一下標記可行路徑.

程式碼實現

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int INF=0x3f3f3f3f;
const int maxn = 1e6+7;

int tot;
int dist[maxn],head[maxn],pre[maxn];
bool vis[maxn] , flag;
int ans[maxn],len=0;

struct node
{
    int u;
    int v;
    int c;
    int ne;
} edge[maxn];

struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c) {}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};

void init()
{
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    memset(dist,INF,sizeof(dist));
    memset(head,-1,sizeof(head));
    tot=0;
    flag=false;
}
void addedge(int u,int v,int cost)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].c=cost;
    edge[tot].ne=head[u];
    head[u]=tot++;
}

void Dijkstra(int n,int start)//點的編號從1開始
{
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].ne)
        {
            int v=edge[i].v;
            int cost=edge[i].c;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                pre[v] = u;
                dist[v] = dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}

void print_ans(int point)
{
    if(point==1)
    {
        cout<<" "<<0;
        return ;
    }
    print_ans(point-1);
    cout<<" "<<ans[point]-1;
}

void dfs(int tmp,int pree)
{
    ans[++len]=tmp;
    vis[tmp]=1;
    if(tmp==2)
    {
        flag=true;
        cout<<len;
        print_ans(len);
        cout<<endl;
        return ;
    }
    for(int i=head[tmp];i!=-1;i=edge[i].ne)
    {
        int v=edge[i].v;
        if(pre[tmp]==v || vis[v]) continue;
        dfs(v,tmp);
        if(flag) return ;
    }
    --len;
}

int main()
{
    IO;
    init();
    int n,m;
    int u,v,c;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v>>c;
        addedge(u+1,v+1,c);
        addedge(v+1,u+1,c);
    }
    Dijkstra(n,2);
    memset(vis,0,sizeof(vis));
    dfs(1,-1);
    if(!flag) cout<<"impossible"<<endl;
    return 0;
}

Ps:Dijkstar 優化演算法模板(from kuangbin)

/*
* 使用優先佇列優化Dijkstra演算法
* 複雜度O(ElogE)
* 注意對vector<Edge>E[MAXN]進行初始化後加邊
*/
const int INF=0x3f3f3f3f;
const int MAXN=1000010;
struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c) {}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//點的編號從1開始
{
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)dist[i]=INF;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0; i<E[u].size(); i++)
        {
            int v=E[tmp.v][i].v;
            int cost=E[u][i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}