点击打开链接
//
// main.cpp
// test
//
// Created by 吴有堃 on 2017/9/11.
// Copyright © 2017年 吴有堃. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn= 1005;
int T,N;
bool v[maxn];
int d[maxn];
int Arr[maxn][maxn]={0};
void solve()
{
memset(v, false, sizeof(v));
for(int i=1;i<=N;i++) d[i]=(i==1?0:inf);
for(int i=1;i<=N;i++){
int x=0, m=inf;
for(int y=1; y<=N;y++) if(!v[y]&&d[y]<m) m=d[x=y];
v[x]=1;
for(int y=1;y<=N;y++){
if(d[y]>(d[x]+Arr[x][y])){
d[y]=d[x]+Arr[x][y];
}
}
}
}
int main(int argc, const char * argv[]) {
scanf("%d %d",&T,&N);
memset(Arr,inf, sizeof(Arr));
int a,b,weight=0;
for(int i=0;i<T;i++){
scanf("%d %d %d",&a,&b,&weight);
if(weight<Arr[a][b]) Arr[a][b]=Arr[b][a]=weight;
}
solve();
printf("%d\n",d[N]);
return 0;
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<cstring>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <queue>
#define inf 0x3f3f3f3f
#define LL long long
#include <cstring>
using namespace std;
#define pa pair<int, int>
const int maxn=1e5+5;
struct Node{
int to, next, v;
}edge[maxn];
int cnt=1, head[maxn], n, dis[maxn];
int Arr[1005][1005];
void add(int u, int v, int w)///固定的套路
{
edge[cnt].to = v;//表示这条边指向的点
edge[cnt].next = head[u]; //边指的下一条边标记
head[u] = cnt;
edge[cnt].v = w; //这条边的权重
cnt++;
}
void Dijkstra()
{
priority_queue<pa, vector<pa>, greater<pa> >q; // 最小堆
int i, now;
for(i=1;i<=n;i++) dis[i]=inf;
dis[1]=0;
q.push(make_pair(0, 1)); //第一个是权值, 第二个是点
while (!q.empty()) {
now =q.top().second; q.pop();//每次找出最小的
for(i=head[now];i!=-1; i= edge[i].next){
if(dis[now]+edge[i].v< dis[edge[i].to]){// 更新
dis[edge[i].to] = dis[now]+edge[i].v;
q.push(make_pair(dis[edge[i].to], edge[i].to));
}
}
}
}
int main()
{
int i=0, j=0,a=0,b=0,weight,T=0;
memset(Arr, inf, sizeof(Arr));
memset(head, -1, sizeof(head));
scanf("%d %d",&T,&n);
for(i=0; i<T;i++){
scanf("%d %d %d",&a,&b,&weight);
if(weight<Arr[a][b]){
Arr[a][b]=Arr[b][a]=weight;
}
}
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++){
if(Arr[i][j]<inf){
add(i,j,Arr[i][j]);
add(j,i,Arr[j][i]);
}
}
}
Dijkstra();
printf("%d\n",dis[n]);
return 0;
}
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string> #include<cstring> #include<string.h> #include<math.h> #include<algorithm> #include <queue> #define inf 0x3f3f3f3f #define LL long long using namespace std; const int maxn=1e3+5; struct Node{ int x,d; }; vector<Node> Eg[maxn]; int Dist[maxn],N=0,Arr[maxn][maxn]; bool operator<(Node x, Node y) //重载了排序函数 { return x.d<y.d; } void Dijkstra(int s) { int i=0; for(i=0;i<=N;i++) Dist[i]=inf; Dist[s]=0; priority_queue<Node> Q; Node temp1,temp2; temp1.x=s, temp1.d= Dist[s]; Q.push(temp1); while (!Q.empty()) { temp1=Q.top(); Q.pop(); for(i=0; i<Eg[temp1.x].size();i++){ temp2=Eg[temp1.x][i]; //temp2是拿出来的,相当于d[y] if(Dist[temp1.x]+temp2.d < Dist[temp2.x]){ Dist[temp2.x] =Dist[temp1.x] +temp2.d; Q.push(temp2); } } } } int main() { int i=0,j=0,T=0,a=0,b=0,w=0; Node t1, t2; memset(Arr, inf, sizeof(Arr)); scanf("%d %d",&T,&N); for(i=0;i<=N;i++) Eg[i].clear(); for(i=0;i<T;i++){ scanf("%d %d %d",&a, &b, &w); if(w<Arr[a][b]) Arr[a][b]=Arr[b][a]=w; } /* for(i=0;i<=N;i++){ for(j=0;j<=N;j++){ cout<<Arr[i][j]<<" "; } cout<<endl; } */ for(i=1;i<=N;i++){ for(j=1;j<i;j++){ if(Arr[i][j]<inf){ t1.x=j; t1.d=Arr[i][j]; Eg[i].push_back(t1); t2.x=i; t2.d=Arr[i][j]; Eg[j].push_back(t2); } } } Dijkstra(1); printf("%d\n",Dist[N]); return 0; }