標籤:style http os io for 問題 ar div
題意:有一個公交系統的收費標準如下表:
然後問:給出 這些L1~4 & C1~4的值,然後 N個站,列出每個站的X座標,然後詢問M次,問兩個月台的最小花費題解:那麼這裡很明顯是最短路問題,有一點的麻煩就在於建圖,那麼我們可以對於所有的點,用兩個for迴圈,算出兩兩之間的距離,就可以得到花費是多少,同時建邊,然後對於每次詢問的點,我們就spfa一次就OK
<span style="font-size:14px;">#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <cstring>using namespace std;#define INF 0xffffffffffffff#define MAX 105#define LL __int64int N,M;LL L1,L2,L3,L4,C1,C2,C3,C4;LL X[MAX];struct Edge{ int to,next; LL cost;}edge[MAX*MAX];int head[MAX],tol;void add(int u,int v,LL cost){ edge[tol].to = v; edge[tol].cost = cost; edge[tol].next = head[u]; head[u] = tol++;}void del() //處理建邊{ LL cost,dis; for(int i = 1; i <= N; i ++){ for(int j = i+1; j <= N; j ++){ if(X[i] > X[j]) dis = X[i]-X[j]; else dis = X[j]-X[i]; if(dis > L4) cost = INF; else if(dis > L3) cost = C4; else if(dis > L2) cost = C3; else if(dis > L1) cost = C2; else cost = C1; add(i,j,cost); add(j,i,cost); } }}LL dis[MAX];bool flag[MAX];LL spfa(int src,int D){ for(int i = 1; i <= N; i ++) dis[i] = INF; memset(flag,false,sizeof(flag)); dis[src] = 0; flag[src] = true; queue<int>q; q.push(src); while(!q.empty()) { int u = q.front(); q.pop(); flag[u] = false; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; LL cost = edge[i].cost; if(cost + dis[u] < dis[v]) { dis[v] = cost+dis[u]; if(!flag[v]) { q.push(v); flag[v] = true; } } } } return dis[D];}int main(){ int T; scanf("%d",&T); for(int cas = 1; cas <= T; cas ++) { scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4,&C1,&C2,&C3,&C4); scanf("%d%d",&N,&M); for(int i = 1; i <= N; i ++) scanf("%I64d",&X[i]); memset(head,-1,sizeof(head)); tol = 0; del(); printf("Case %d:\n",cas); int a,b; LL ans = 0; for(int i = 0; i < M; i ++) { scanf("%d%d",&a,&b); ans = spfa(a,b); if(ans >= INF) printf("Station %d and station %d are not attainable.\n",a,b); else printf("The minimum cost between station %d and station %d is %I64d.\n",a,b,ans); } } return 0;}</span>那麼這裡的話,還要注意的是 因為座標值比較大,我們用 64位來儲存