杭电多校第七场 Age of Moyu(BFS+DFS)

Age of Moyu

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2539    Accepted Submission(s): 764


 

Problem Description

Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M shipping lines. The ports are numbered 1 to N. Each line is occupied by a Weitian. Each Weitian has an identification number.

The i-th (1≤i≤M) line connects port Ai and Bi (Ai≠Bi) bidirectionally, and occupied by Ci Weitian (At most one line between two ports).

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 1 XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of 1 XiangXiangJi. In a case where Mr.Quin changed from some Weitian A's line to another Weitian's line changes to Weitian A's line again, the additional cost is incurred again.

Mr.Quin is now at port 1 and wants to travel to port N where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can’t travel to port N, print −1instead)

Input

There might be multiple test cases, no more than 20. You need to read till the end of input.

For each test case,In the first line, two integers N (2≤N≤100000) and M (0≤M≤200000), representing the number of ports and shipping lines in the city.

In the following m lines, each contain three integers, the first and second representing two ends Ai and Bi of a shipping line (1≤Ai,Bi≤N) and the third representing the identification number Ci (1≤Ci≤1000000) of Weitian who occupies this shipping line.

Output

For each test case output the minimum required cost. If Mr.Quin can’t travel to port N, output −1 instead.

Sample Input

3 3 1 2 1 1 3 2 2 3 1 2 0 3 2 1 2 1 2 3 2

Sample Output

1 -1 2
Source

2018 Multi-University Training Contest 7

题意:从1走向n,每条边的边权都为1,每条边都有一个类型,走连续类型的边花费为1,输出最小的花费

思路:以BFS为主体,每搜到一个点,在DFS出所有与这条边类型相同的边。实际上就是先搜1步到达的点,在搜2步到达的点,每搜一条边就标记一下边,把点加入队列

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
const int MAXM = 400005;
typedef long long LL;
struct node1
{
    int v,c,next;
}edge[MAXM];
int head[MAXN],cnt;
int n,flag,ans;
bool vis[MAXM];
inline void addedge(int u,int v,int c)
{
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].next = head[u];
    head[u] = cnt++;
};
inline int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
struct node2
{
    int u,step;
    node2(){}
    node2(int _u,int _step){u = _u,step = _step;};
};
queue<node2> q;

void DFS(int u,int c,int step)
{
    for(int i = head[u]; i != -1; i = edge[i].next) {
        if(!vis[i] && c == edge[i].c) {
            if(edge[i].v == n) {
                flag = 1;
                ans = step;
                return;
            }
            q.push(node2(edge[i].v,step));
            vis[i] = 1;
            vis[i ^ 1] = 1;
            DFS(edge[i].v,c,step);
            if(flag) return;
        }
    }
}
void BFS()
{
    int v;
    struct node2 now;
    while(!q.empty()) q.pop();
    q.push(node2(1,0));
    while(!q.empty()) {
        now = q.front();
        q.pop();
        for(int i = head[now.u]; i != -1; i = edge[i].next) {
            if(!vis[i]) {
                v = edge[i].v;
                if(v == n) {
                    flag = 1;
                    ans = now.step + 1;
                    return;
                }
                q.push(node2(v,now.step + 1));
                vis[i] = 1;
                vis[i ^ 1] = 1;
                DFS(v,edge[i].c,now.step + 1);
                if(flag) return;
            }
        }
    }
}
int main(void)
{
    int m;
    int u,v,c;
    while(scanf("%d %d",&n,&m) != EOF) {
        memset(head,-1,sizeof(head));
        cnt = 0;
        for(int i = 0; i < m; ++i) {
            u = read(),v = read(),c = read();
            addedge(u,v,c);
            addedge(v,u,c);
        }
        memset(vis,0,sizeof(vis));
        flag = 0;
        BFS();
        if(flag) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81675055