题目链接
代码:
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std
;
typedef long long ll
;
const int inf
=1<<27;
const int maxn
=1010;
struct node
{
int v
,w
;
node(int a
,int b
){v
=a
;w
=b
;}
friend bool operator<(node a
,node b
){
return a
.w
>b
.w
;
}
};
int t
,n
,a
,b
,w
,dis
[maxn
];
priority_queue
<node
> q
;
vector
<node
> g
[maxn
];
bool vis
[maxn
]={false};
void dijk()
{
fill(dis
,dis
+maxn
,inf
);
dis
[1]=0;
q
.push(node(1,0));
while(!q
.empty())
{
int u
=q
.top().v
;
q
.pop();
if(vis
[u
]) continue;
vis
[u
]=1;
for(int i
=0;i
<g
[u
].size();i
++)
{
int v
=g
[u
][i
].v
;
int w
=g
[u
][i
].w
;
if(!vis
[v
])
{
dis
[v
]=min(dis
[v
],dis
[u
]+w
);
q
.push(node(v
,dis
[v
]));
}
}
}
}
int main()
{
cin
>>t
>>n
;
while(t
--)
{
cin
>>a
>>b
>>w
;
g
[a
].push_back(node(b
,w
));
g
[b
].push_back(node(a
,w
));
}
dijk();
cout
<<dis
[n
];
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-2166.html