题意分析
裸地SPFA
代码总览
#include<bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 10;
typedef struct{
int to, nxt, w;
} Edge;
Edge e[nmax<<1];
int head[nmax<<1];
int dis[(int)1e4+100];
int times[(int)1e4+100];
int n ,m , tot = 0;
void add(int u, int v, int w){
e[tot].to = v;
e[tot].nxt = head[u];
e[tot].w = w;
head[u] = tot++;
}
bool spfa(){
memset(times,0,sizeof times);
memset(dis,0x3f3f3f3f,sizeof dis); dis[1] = 0;
bitset<nmax> inque; inque.reset();
queue<int> q; q.push(1); inque.set(1); times[1] ++;
while(!q.empty()){
int u = q.front(); q.pop(); inque.reset(u);
for(int i = head[u]; i != -1; i = e[i].nxt){
int v = e[i].to;
if(dis[v] > dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
if(!inque.test(v)){
q.push(v);
inque.set(v);
}
times[v] ++;
if(times[v] >= n) return false;
}
}
}
return dis[n] != 0x3f3f3f3f;
}
int main(){
while(scanf("%d %d",&n ,&m)!=EOF && n&& m){
memset(head,-1,sizeof head); tot = 0;
int u,v,w1,w2;
for (int i = 0; i < m; ++ i) {
scanf("%d %d %d %d",&u, &v, &w1, &w2);
add(u,v,w1);
add(v,u,w2);
}
bool isok = spfa();
if(isok) printf("%d\n",dis[n]);
else printf("No such path\n");
}
return 0;
}