HDU 2544

Problem Description

Input

Output

Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output
3
2

``````#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
int nex[200005];
int to[200005];
int dis[200005];
int w[200005];
bool v[100005];
void input(int &x){
char c=getchar(); x=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0') x=x*10+c-48, c=getchar();
}
int tot;
inline void add(int x,int y,int z){
++tot;
to[tot]=y;
w[tot]=z;
}
inline void spfa(int u){
queue<int>q;
memset(dis,0x7f7f7f,sizeof(dis));
v[u]=1;
dis[u]=0;
q.push(u);
while(!q.empty()){
int dmf=q.front();
q.pop();
int hxr=to[i];
if(dis[hxr]>dis[dmf]+w[i])
{
dis[hxr]=dis[dmf]+w[i];
if(!v[hxr])
{
v[hxr]=1;
q.push(hxr);
}
}
}
v[dmf]=0;
}
printf("%d\n",dis[n]);
}
int main(){
while(scanf("%d%d",&n,&m)==2){
memset(dis,0,sizeof(dis));
tot=0;
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
memset(to,0,sizeof(to));
memset(nex,0,sizeof(nex));
if(n==0&&m==0) return 0;
for(int i=1;i<=m;i++)
{
int u ,v,len;
input(u);
input(v);
input(len);
}
spfa(1);

}
}``````

2022-12-06 15:46

2022-12-26 19:44

2022-12-25 02:02

2022-12-19 23:18

2022-12-22 14:03

2022-12-26 12:49

2022-12-28 01:03