ccf 2017 12 - 4 行车路线
Posted qq136155330
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ccf 2017 12 - 4 行车路线相关的知识,希望对你有一定的参考价值。
附上代码:
#include<stdio.h> #include<string.h> #define inf 0x7fffffff #include<iostream> #include<algorithm> #include<queue> using namespace std; typedef long long ll; const int MAXM = 1e6 + 5; int n, m; typedef struct NODE { int to; ll temp; ll cost; int ff; friend bool operator < (const NODE &a, const NODE &b) { return a.cost > b.cost; } }node; typedef struct Edge { int from;///起点 ll temp;///记录权值 int f;///标记小路 int next; int to; ll w; } edge; edge maps[MAXM]; int head[505]; ll dist[505]; int vids[505]; int cnt; void creat () { for(int i=0; i<505; i++) head[i]=-1; cnt=0; } void add(int t,int u,int v,ll w) { maps[cnt].f = t; maps[cnt].from = u; maps[cnt].to=v; maps[cnt].w=w; maps[cnt].next=head[u];///一开始放置为-1,-1的条件就可以跳出 ///下一步接着储存,head[u]的值,就是前面的位置 head[u]=cnt++;///head[u]会得到这条线的值 } ll dijkstra(int s,int t) { for(int i=0; i<505; i++) { vids[i]=0; dist[i]=inf; } priority_queue<node>que; node begins; begins.to = s; begins.temp = 0; begins.cost = 0; begins.ff = 0; que.push(begins); while(!que.empty()) { node ends=que.top(); que.pop(); if(!vids[ends.to]) { vids[ends.to]=1; dist[ends.to]=ends.cost; for(int i = head[ends.to]; ~i; i = maps[i].next) { int to = maps[i].to; ll w = maps[i].w; int flag = maps[i].f; if(!vids[to]) { node ans; ans.to=to; if(ends.ff == 0 && flag == 0) { ans.cost = ends.cost + w; ans.ff = flag; ans.temp = 0; } else if(ends.ff == 1 && flag == 0) { ans.cost = ends.cost + w; ans.ff = flag; ans.temp = 0; } else if(ends.ff == 1 && flag == 1) { ans.cost = ends.cost - (ends.temp * ends.temp) + (ends.temp + w) * (ends.temp + w); ans.ff = flag; ans.temp = ends.temp + w; } else if(ends.ff == 0 && flag == 1) { ans.cost = ends.cost + (w * w); ans.ff = flag; ans.temp = w; } que.push(ans); } } } } if(dist[t] == inf) { return -1; } else { return dist[t]; } } int main() { while(~scanf("%d%d",&n, &m)) { creat(); int t, a, b; long long c; for (int i = 0; i < m; i ++) { scanf("%d%d%d%lld",&t, &a, &b, &c); add(t, a, b, c); add(t, b, a, c); } ll re = dijkstra(1, n); printf("%lld ",re); } return 0; }
以上是关于ccf 2017 12 - 4 行车路线的主要内容,如果未能解决你的问题,请参考以下文章