hdu 2923 Einbahnstrasse(Floyd+map)

來源:互聯網
上載者:User

http://acm.split.hdu.edu.cn/showproblem.php?pid=2923

題目大意:
給出城市數,破車數和道路數,然後跟著m條路線,箭頭為指向,數字為花費。求出從公司派出車,到每個城市回收破車的最小花費,要注意每輛車只能回收一輛破車。

用map記錄,注意有重邊,然後Floyd求出 d[u][v] 的花費。

#include<iostream>#include<cstdio>#include<map>#define INF 0x3f3f3f3fusing namespace std;const int maxn=105;int tt=0,sum,n,c,m,cnt,d[maxn][maxn];char str[1005][maxn];map<string,int>mp;void Floyd(){    for(int k=1;k<=n;k++)      for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)          d[i][j]=min(d[i][j],d[i][k]+d[k][j]);}int main(){    while(~scanf("%d%d%d",&n,&c,&m)&&(n||c||m))    {        sum=0;        mp.clear();        for(int i=0;i<=n;i++)          for(int j=0;j<=n;j++)            d[i][j]=INF;        for(int i=0;i<=c;i++)          scanf("%s",str[i]);        cnt=0;        for(int i=0;i<m;i++)        {            int w;            char from,to,s1[15],s2[15];            scanf("%s %c-%d-%c %s",s1,&from,&w,&to,s2);            if(!mp[s1])              mp[s1]=++cnt;            if(!mp[s2])              mp[s2]=++cnt;            int u=mp[s1],v=mp[s2];            if(from=='<'&&d[v][u]>w)              d[v][u]=w;            if(to=='>'&&d[u][v]>w)              d[u][v]=w;        }        Floyd();        int src=mp[str[0]];        for(int i=1;i<=c;i++)          sum+=d[src][mp[str[i]]]+d[mp[str[i]]][src];        printf("%d. %d\n",++tt,sum);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.