# 【CCF-CSP201712-4】行车路线

CSP上的一道题，很巧的一道题，连续走小路的话，代价为连续小路的权值的和的平方，走大路的话，代价就直接为大路的权值。

``````#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define CLR(a) memset(a, 0, sizeof(a))
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, a, b) for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000009;
const int N= 1e3 +10;

ll dist[N][2];
ll g[N][N], g0[N][N];
int n, m;
int t, a, b;
ll c;

void Floyd() {
REP(k, 1, n) {
REP(i, 1, n) {
REP(j, 1, n) {
g[i][j]=min(g[i][j], g[i][k]+g[k][j]);
}
}
}
REP(i, 1, n) {
REP(j, 1, n){
if (g[i][j]==LL_INF) continue;
g[i][j]=g[i][j]*g[i][j];
}
}
}

struct node {
int u;
ll d;
int p;
bool operator < (const node& rhs) const {
return d>rhs.d;
}
};

void Dijkstra() {
memset(dist, 0x3f, sizeof(dist));
priority_queue<node> q;
q.push({1,0,0});
dist[1][0]=0;
while(!q.empty()) {
node t=q.top();q.pop();

int u=t.u;
if (t.d>dist[u][t.p]) continue;
//大路
REP(i, 1, n) {
if (g0[u][i]==LL_INF) continue;
if (t.d+g0[u][i]<dist[i][0]) {
dist[i][0]=t.d+g0[u][i];
q.push({i,dist[i][0],0});
}
}
//小路
if (t.p) continue;
REP(i, 1, n) {
if (g[u][i]==LL_INF) continue;
if (t.d+g[u][i]<dist[i][1]) {
dist[i][1]=t.d+g[u][i];
q.push({i,dist[i][1],1});
}
}
}

}

int main() {
scanf("%d%d", &n, &m);

memset(g, 0x3f, sizeof(g));
memset(g0, 0x3f, sizeof(g0));

REP(i, 1, m) {
scanf("%d%d%d%d", &t, &a, &b, &c);
if (t&&c<g[a][b]) g[a][b]=g[b][a]=c;
if (!t&&c<g0[a][b]) g0[a][b]=g0[b][a]=c;
}

Floyd();

Dijkstra();

cout<<min(dist[n][1],dist[n][0])<<endl;
//cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";
return 0;
}``````