Description
lqp18_31和1tthinking经常出题来虐ftiasch。有一天, lqp18_31搞了一个有向图,每条边的长度都是1。 他想让ftiasch求出点1到点 N 的最短路。"水题啊。", ftiasch这么说道。
所以1tthinking把某些边的长度增加了1(也就是说,每条边的长度不是1就是2)。现在,可怜的ftiasch要向你求助了。
Input
第1行,两个整数 N (1 ≤ N ≤ 105) 和 M (1 ≤ M ≤ 106), 点和边的数量。
第2到 M + 1行: 三个整数 Ui, Vi, Wi (1 ≤ Wi ≤ 2), 从点 Ui 到 Vi 长度为 Wi 的边。
Output
一个整数,表示点1到点N的最短路。数据保证至少存在一条路径。
Sample Input
3 3
1 2 1
2 3 1
1 3 2
Sample Output
2
HINT
woc……加上快读各种WA+TLE……
去掉快读……各种AC……
????
dij比spfa快了40倍啊woc!
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int
N=100005,
M=1000005;
int Ecnt,n,m;
int dist[N];
bool vis[N];
struct Heap{int num,val;};
#define qq(a,b) (Heap){a,b}
priority_queue<Heap,vector<Heap> >Q;
bool operator <(Heap x,Heap y){return x.val>y.val;}
struct Edge{
int next,to,val;
}E[M];int head[N];
void add(int u,int v,int w){
E[++Ecnt].next=head[u];
E[Ecnt].to=v;
E[Ecnt].val=w;
head[u]=Ecnt;
}
void dijkstra(int s,int t){
memset(vis,0,sizeof(vis));
memset(dist,100,sizeof(dist));
dist[s]=0;
Q.push(qq(s,0));
while ((!Q.empty())){
Heap mi=Q.top();Q.pop();
if (vis[mi.num]) continue;
vis[mi.num]=1;
for (int j=head[mi.num];j;j=E[j].next){
int q=E[j].to;
if (vis[q]) continue;
if (E[j].val+mi.val<dist[q]){
dist[q]=E[j].val+mi.val;
Q.push(qq(q,dist[q]));
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
int x,y,z;
while (m--){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dijkstra(1,n);
printf("%d\n",dist[n]);
return 0;
}