hdu6386(dijkstra+堆优化+记录状态)

Age of Moyu

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

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,属于同一个人这不需要花费,但是第一次遇到第一个人要花费1.求1到n的最小花费。

解析:我们只需要记录每次当前点它之前最短路的前一条边是属于谁的,若是相同者cost=0,反之cost=1;这过程中跑一边dijkstra+堆优化就可以

#include<bits/stdc++.h>
using namespace std;
 
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=1e5+5;
int n,m;

struct qnode{
    int v,dis;
    qnode(int v=0,int dis=0):v(v),dis(dis) {}
    bool operator<(const qnode &r)const
    {
        return dis>r.dis;
    }
};
struct Edge{
	int v,w;
	Edge(int v=0,int w=0):v(v),w(w) {} 
};
vector<Edge> ve[maxn];
int d[maxn],ow[maxn];
bool vis[maxn];

void addEdge(int u,int v,int w)
{
	ve[u].push_back(Edge(v,w));
}
void dijkstra(int start)
{
	mem(vis,false);
	mem(d,inf);
	mem(ow,-1);
	priority_queue<qnode> q;
	d[start]=0;
	
	q.push(qnode(start,0));
	qnode tmp;
	while(!q.empty())
	{
		tmp=q.top();
		q.pop();
		int u=tmp.v;
		if(vis[u])continue;
		vis[u]=true;
		for(int i=0; i<ve[u].size(); i++)
		{
			int v=ve[u][i].v;
			int w=ve[u][i].w;
			int cost;
			
			if(w!=ow[u])
			{
				cost=1;
			}
			else cost=0;
			if(!vis[v]&&d[v]>d[u]+cost)
			{
				d[v]=d[u]+cost;
				ow[v]=w;
				q.push(qnode(v,d[v]));
			}
		}
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1; i<=n; i++)ve[i].clear();
		for(int i=0; i<m; i++)
		{
			int u,v,w;scanf("%d%d%d",&u,&v,&w);
			addEdge(u,v,w);
			addEdge(v,u,w);
		}
		
		dijkstra(1);
		if(d[n]==inf)puts("-1");
		else printf("%d\n",d[n]);
	}
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值