日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HDU Today

發(fā)布時(shí)間:2024/10/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Today 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

?

Problem Description

經(jīng)過(guò)錦囊相助,海東集團(tuán)終于度過(guò)了危機(jī),從此,HDU的發(fā)展就一直順風(fēng)順?biāo)?#xff0c;到了2050年,集團(tuán)已經(jīng)相當(dāng)規(guī)模了,據(jù)說(shuō)進(jìn)入了錢(qián)江肉絲經(jīng)濟(jì)開(kāi)發(fā)區(qū)500強(qiáng)。這時(shí)候,XHD夫婦也退居了二線(xiàn),并在風(fēng)景秀美的諸暨市浬浦鎮(zhèn)陶姚村買(mǎi)了個(gè)房子,開(kāi)始安度晚年了。
這樣住了一段時(shí)間,徐總對(duì)當(dāng)?shù)氐慕煌ㄟ€是不太了解。有時(shí)很郁悶,想去一個(gè)地方又不知道應(yīng)該乘什么公交車(chē),在什么地方轉(zhuǎn)車(chē),在什么地方下車(chē)(其實(shí)徐總自己有車(chē),卻一定要與民同樂(lè),這就是徐總的性格)。
徐總經(jīng)常會(huì)問(wèn)蹩腳的英文問(wèn)路:“Can you help me?”。看著他那迷茫而又無(wú)助的眼神,熱心的你能幫幫他嗎?
請(qǐng)幫助他用最短的時(shí)間到達(dá)目的地(假設(shè)每一路公交車(chē)都只在起點(diǎn)站和終點(diǎn)站停,而且隨時(shí)都會(huì)開(kāi))。

?

?

Input

輸入數(shù)據(jù)有多組,每組的第一行是公交車(chē)的總數(shù)N(0<=N<=10000);
第二行有徐總的所在地start,他的目的地end;
接著有n行,每行有站名s,站名e,以及從s到e的時(shí)間整數(shù)t(0<t<100)(每個(gè)地名是一個(gè)長(zhǎng)度不超過(guò)30的字符串)。
note:一組數(shù)據(jù)中地名數(shù)不會(huì)超過(guò)150個(gè)。
如果N==-1,表示輸入結(jié)束。

?

?

Output

如果徐總能到達(dá)目的地,輸出最短的時(shí)間;否則,輸出“-1”。

?

?

Sample Input

6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1

?

?

Sample Output

50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 雖然偶爾會(huì)迷路,但是因?yàn)橛辛四愕膸椭?**和**從此還是過(guò)上了幸福的生活。 ――全劇終――

?

?

Author

lgx

?

?

Source

ACM程序設(shè)計(jì)_期末考試(時(shí)間已定!!)

?

?

Recommend

lcy???|???We have carefully selected several similar problems for you:??2544?1874?1217?2680?1142?

?

C++版本一

/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUGusing namespace std; typedef long long ll; const int N=50; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int n; int edgenum; int head[20005]; char ex[35],ey[35]; int vis[155]; int dis[155];struct Edge {int from,to,val,next; }edge[20005]; void addedge(int u,int v,int w) {Edge E={u,v,w,head[u]};edge[edgenum]=E;head[u]=edgenum++; } map<string,int>mp; int t;int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifwhile(scanf("%d",&n)&&(n!=-1)){edgenum=0;memset(head,-1,sizeof(head));mp.clear();//一定要清零!getchar();scanf("%s%s",ex,ey);mp[ex]=1;t=2;if(!mp[ey]){mp[ey]=t++;}char a[35],b[35];int c;for(int i=1;i<=n;i++){getchar();scanf("%s%s%d",a,b,&c);if(!mp[a]){mp[a]=t++;}if(!mp[b]){mp[b]=t++;}addedge(mp[a],mp[b],c);addedge(mp[b],mp[a],c);}queue<int>q;q.push(1);memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));vis[1]=1;dis[1]=0;while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(dis[v]>dis[u]+edge[i].val){dis[v]=dis[u]+edge[i].val;if(vis[v]==0){vis[v]=1;q.push(v);}}}}if(dis[mp[ey]]==INF)printf("-1\n");elseprintf("%d\n",dis[mp[ey]]);}return 0; }

C++版本二

剛開(kāi)始沒(méi)有考慮起終點(diǎn)不在路徑內(nèi)的情況

/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUGusing namespace std; typedef long long ll; const int N=50; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int n,m; char s[N],t[N]; struct node {int e,w; }; struct cmp{bool operator()(const node &a,const node &b){return a.w>b.w;} }; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifwhile(~scanf("%d",&n)&&n!=-1){scanf("%s%s",s,t);priority_queue<node,vector<node>,cmp>que;vector<node>v[200];int vis[200]={0};node x;map<string ,int >ma;char a[N],b[N];int c,cnt=0;if(!ma[s]){ma[s]=++cnt;}if(!ma[t]){ma[t]=++cnt;}for(int i=1;i<=n;i++){scanf("%s%s%d",a,b,&c);if(!ma[a]){ma[a]=++cnt;}if(!ma[b]){ma[b]=++cnt;}x.e=ma[b];x.w=c;v[ma[a]].push_back(x);x.e=ma[a];v[ma[b]].push_back(x);}x.e=ma[s];x.w=0;que.push(x);while(!que.empty()){x=que.top();que.pop();vis[x.e]=1;if(x.e==ma[t])break;for(int i=0,j=v[x.e].size();i<j;i++){node q;q.e=v[x.e][i].e;if(vis[q.e])continue;q.w=x.w+v[x.e][i].w;que.push(q);}}if(x.e==ma[t])cout << x.w << endl;elsecout << -1 << endl;}//cout << "Hello world!" << endl;return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的HDU Today的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。