问题描述
小明和小芳出去乡村玩,小明负责开车,小芳来导航。
小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走 s公里小明会增加 s 2的疲劳度。
例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2) 2+2+2 2=16+2+4=22。
现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走 s公里小明会增加 s 2的疲劳度。
例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2) 2+2+2 2=16+2+4=22。
现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
输入格式
输入的第一行包含两个整数
n,
m,分别表示路口的数量和道路的数量。路口由1至
n编号,小明需要开车从1号路口到
n号路口。
接下来 m行描述道路,每行包含四个整数 t, a, b, c,表示一条类型为 t,连接 a与 b两个路口,长度为 c公里的双向道路。其中 t为0表示大道, t为1表示小道。保证1号路口和 n号路口是连通的。
接下来 m行描述道路,每行包含四个整数 t, a, b, c,表示一条类型为 t,连接 a与 b两个路口,长度为 c公里的双向道路。其中 t为0表示大道, t为1表示小道。保证1号路口和 n号路口是连通的。
输出格式
输出一个整数,表示最优路线下小明的疲劳度。
样例输入
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
样例输出
76
样例说明
从1走小道到2,再走小道到3,疲劳度为5
2=25;然后从3走大道经过4到达5,疲劳度为20+30=50;最后从5走小道到6,疲劳度为1。总共为76。
80分.. 赛后还没想怎么优化.. 以后再解决吧
#include<bits/stdc++.h> using namespace std; const long long INF = 1e18; int n,m; struct node{ int to; long long cost; }; struct edge{ int id; long long cost; int len; bool operator < (const edge & a)const { return cost > a.cost; } }; vector<node> G[510][2]; long long d[510]; priority_queue<edge>que; long long p2(long long a){ return a * a; } int solve(){ fill(d,d+n+1,INF); d[1] = 0; que.push({1,0,0}); while(!que.empty() ){ edge a = que.top(); que.pop(); int v = a.id; if(v == n) break; if(d[v] < a.cost) continue; for(int i=0;i<G[v][0].size();i++){ node e = G[v][0][i]; if(d[e.to] > d[v] + e.cost){ d[e.to] = d[v] + e.cost; que.push(edge{e.to,d[e.to],0}); } } for(int i=0;i<G[v][1].size();i++){ node e = G[v][1][i]; int cost = p2(a.len+e.cost)-p2(a.len); if(d[e.to] > d[v] + cost){ d[e.to] = d[v] + cost; que.push(edge{e.to,d[e.to],a.len+e.cost}); } } } return d[n]; } int main () { scanf("%d %d",&n,&m); for(int i=1;i<=m;i++){ int t,a,b; long long c; scanf("%d %d %d %lld",&t,&a,&b,&c); G[a][t].push_back({b,c}); G[b][t].push_back({a,c}); } cout << solve() <<endl; return 0; }