[Ox-off] [Peking University postgraduate retest] I Wanna Go Home [dijkstra]

Title Description

    The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.     "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."     Would you please tell Mr. M at least how long will it take to reach his sweet home?

Enter a description:

    The input contains multiple test cases.
    The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
    The second line contains one integer M (0<=M<=10000), which is the number of roads.
    The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
    Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i. 
    To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2. 
    Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.

Output Description:

    For each test case, output one integer representing the minimum time to reach home.
    If it is impossible to reach home according to Mr. M's demands, output -1 instead.
Example 1

Entry

copy
2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0

Export

copy
100
90
540

 

Single-source shortest path problem, the bare directly sets the Dijkstra (conventionally added heap optimization), plus a 2 to 1 is determined to not urban city updates dis.
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 700;
const int M = 80000;
int n,m,tol;
struct Edge{
    int v,w,next;
}edge[M];
int head[N],vis[N],dis[N];
int lable[N];
void init(){
    tol = 0;
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w){
    edge[tol] = Edge{v,w,head[u]};
    head[u] = tol++;
}
struct Node{
    int u,w;
    bool operator < (const Node a)const{
        return w > a.w;
    }
}; 
priority_queue <Node> q;
void dijkstra(){
    memset(dis,inf,sizeof(dis));
    dis[1] = 0;
    q.push(Node{1,0});
    while(!q.empty()){
        Node a = q.top();
        q.pop();
        int u = a.u,w = a.w;
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u];i != -1;i = edge[i].next){
            int v = edge[i].v,w = edge[i].w;
            if (!vis[v] && dis[v] > dis[u] + w && !(lable[u] == 2 && lable[v] == 1)){
                dis[v] = dis[u] + w;
                q.push(Node{v,dis[v]});
            }
            
        }
    }
}

int main(){
    while (~scanf("%d",&n)&&n){
        scanf("%d",&m);
        init();
        for (int i = 0;i < m;++i){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,in);
            addedge(v,u,w);
        }
        for (int i = 1;i <= n;++i){
            int tmp;
            scanf("%d",&tmp);
            lable[i] = tmp;
        }
        dijkstra();
        if (dis[2] == inf) puts("-1");
            else printf("%d\n",dis[2]);
    }
}

 

Guess you like

Origin www.cnblogs.com/mizersy/p/11613946.html