hdu1690Bus System--解題報告

來源:互聯網
上載者:User

標籤: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位來儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.