当前位置: 首页 > 工具软件 > moyu > 使用案例 >

hdu 6386 Age of Moyu

梁鸿风
2023-12-01


Age of Moyu

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


 

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 

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6396 6395 6394 6393 6392 

 

比赛的时候醉了,写了bfs没有调用,数据过了但是TLE了,代码本身还有点小错误,参考了一下大神的代码A了

#include <map>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define N 100005
using namespace std;
struct xy{
    int u,v,pos,next;
    bool f;
}xy[N*4];
queue<int>p;
int v[N],q[N];
bool f[N];
int n,m,cut,ans;
void dfs(int x,int y,int num)
{
    if(x==n)
    {
        ans=num;
        return;
    }
    if(!f[x])
    {
        f[x]=true;
        v[x]=num;
        p.push(x);
    }
    for(int i=q[x];i!=-1;i=xy[i].next)
    {
        if(xy[i].f)
            continue;
        if(xy[i].pos==y)
        {
            xy[i].f=true;
            dfs(xy[i].v,y,num);
        }
    }
}

int bfs(int start,int end)
{
    int pos;
    p.push(start);
    v[start]=0;
    f[start]=true;
    while(!p.empty())
    {
        int k=p.front();
        p.pop();
        for(int i=q[k];i!=-1;i=xy[i].next)
        {
            if(xy[i].f)
                continue;
            pos=xy[i].v;
            xy[i].f=true;
            dfs(pos,xy[i].pos,v[k]+1);
            if(ans>0)
                break;
        }
        if(ans>0)
            break;
    }
    return ans;
}

int main(){
    while (scanf("%d%d",&n,&m)!=EOF) {
        while (!p.empty()) {
            p.pop();
        }
        cut=0;
        memset(q,-1,sizeof(q));
        memset(f,0,sizeof(f));
        memset(v,0xfffffff,sizeof(v));
        int x,y,pos;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&pos);
            xy[cut].u=x;
            xy[cut].v=y;
            xy[cut].pos=pos;
            xy[cut].next=q[x];
            xy[cut].f=false;
            q[x]=cut++;
            xy[cut].u=y;
            xy[cut].v=x;
            xy[cut].pos=pos;
            xy[cut].next=q[y];
            xy[cut].f=false;
            q[y]=cut++;
        }
        ans=-1;
        ans=bfs(1, n);
        printf("%d\n",ans);
    }
}

 

 类似资料:

相关阅读

相关文章

相关问答