上交三月月赛[SJTU] 1105 path
生活随笔
收集整理的這篇文章主要介紹了
上交三月月赛[SJTU] 1105 path
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這題主要是字符讀入的問題吧,對于NAME的讀入,一直getchar()到',',對于后面的五個屬性from,to,length,people number,light只讀數(shù)字就可以了,而且他們的處理相同,可以用一個函數(shù)來實現(xiàn)
下面是代碼:
1.?????????????????????? #include<iostream> 2.?????????????????????? #include<cstdio> 3.?????????????????????? #include<cstring> 4.?????????????????????? #include<queue> 5.?????????????????????? #include<algorithm> 6.?????????????????????? using namespace std; 7.?????????????????????? struct Edge{ 8.?????????????????????? ????long a,b; 9.?????????????????????? }; 10.??????????????????? struct Edge e[30002]; 11.??????????????????? long pos[1001],next[30002],d[1001]; 12.??????????????????? long w[30002][3]; 13.??????????????????? bool v[1001]; 14.??????????????????? long n,m,s,t,tt,tot; 15.??????????????????? ? 16.??????????????????? long init(void) 17.??????????????????? { 18.??????????????????? ????char c; 19.??????????????????? ????long k=0; 20.??????????????????? ????c=getchar(); 21.??????????????????? ????while (!((c>='0')&&(c<='9'))){c=getchar();} 22.??????????????????? ????while ((c>='0')&&(c<='9')){ 23.??????????????????? ????????k=k*10+c-'0'; 24.??????????????????? ????????c=getchar(); 25.??????????????????? ????} 26.??????????????????? ????return k; 27.??????????????????? } 28.??????????????????? ? 29.??????????????????? void build(long x,long y,long len,long pn,long light) 30.??????????????????? { 31.??????????????????? ????tot++; 32.??????????????????? ????e[tot].a=x;e[tot].b=y;w[tot][0]=len;w[tot][1]=pn;w[tot][2]=light; 33.??????????????????? ????next[tot]=pos[x]; 34.??????????????????? ????pos[x]=tot; 35.??????????????????? } 36.??????????????????? ? 37.??????????????????? void spfa(long s,long t,long QU) 38.??????????????????? { 39.??????????????????? ????queue<long> q; 40.??????????????????? ????memset(v,sizeof(v),0); 41.??????????????????? ????for (long i=1;i<=n;i++)d[i]=1000000000; 42.??????????????????? ????d[s]=0;v[s]=true;q.push(s); 43.??????????????????? ????while (!q.empty()) 44.??????????????????? ????{ 45.??????????????????? ????????long now=q.front();q.pop(); 46.??????????????????? ????????long k=pos[now]; 47.??????????????????? ????????while (k) 48.??????????????????? ????????{ 49.??????????????????? ????????????long y=e[k].b; 50.??????????????????? ????????????if (d[y]>d[now]+w[k][QU]) 51.??????????????????? ????????????{ 52.??????????????????? ????????????????d[y]=d[now]+w[k][QU]; 53.??????????????????? ????????????????if (!v[y]) 54.??????????????????? ????????????????{ 55.??????????????????? ????????????????????q.push(y);v[y]=true; 56.??????????????????? ????????????????} 57.??????????????????? ? 58.??????????????????? ????????????} 59.??????????????????? ????????????k=next[k]; 60.??????????????????? ????????} 61.??????????????????? ????????v[now]=false; 62.??????????????????? ????} 63.??????????????????? ????printf("%ld",d[t]); 64.??????????????????? ????if (QU!=2)cout<<' '; else cout<<endl; 65.??????????????????? ? 66.??????????????????? } 67.??????????????????? ? 68.??????????????????? int main() 69.??????????????????? { 70.??????????????????? ? 71.??????????????????? ? 72.??????????????????? ??cin>>tt; 73.??????????????????? ????while(tt--){ 74.??????????????????? ????????cin>>n>>m>>s>>t; 75.??????????????????? ????????tot=0; 76.??????????????????? ????????memset(pos,0,sizeof(pos)); 77.??????????????????? ????????for (long i=0;i<m;i++) 78.??????????????????? ????????{ 79.??????????????????? ????????????char s; 80.??????????????????? ????????????s=getchar(); while (s!=',') s=getchar(); 81.??????????????????? ????????????long x=init();//cout<<x<<endl; 82.??????????????????? ????????????long y=init();//cout<<y<<endl; 83.??????????????????? ????????????long len=init();//cout<<len<<endl; 84.??????????????????? ????????????long pn=init();//cout<<pn<<endl; 85.??????????????????? ????????????long light=init();//cout<<light<<endl; 86.??????????????????? ????????????build(x,y,len,pn,light); 87.??????????????????? ????????????build(y,x,len,pn,light); 88.??????????????????? ????????} 89.??????????????????? ????????spfa(s,t,0); 90.??????????????????? ????????spfa(s,t,1); 91.??????????????????? ????????spfa(s,t,2); 92.??????????????????? ????} 93.??????????????????? ????return 0; 94.??????????????????? }
下面是代碼:
1.?????????????????????? #include<iostream> 2.?????????????????????? #include<cstdio> 3.?????????????????????? #include<cstring> 4.?????????????????????? #include<queue> 5.?????????????????????? #include<algorithm> 6.?????????????????????? using namespace std; 7.?????????????????????? struct Edge{ 8.?????????????????????? ????long a,b; 9.?????????????????????? }; 10.??????????????????? struct Edge e[30002]; 11.??????????????????? long pos[1001],next[30002],d[1001]; 12.??????????????????? long w[30002][3]; 13.??????????????????? bool v[1001]; 14.??????????????????? long n,m,s,t,tt,tot; 15.??????????????????? ? 16.??????????????????? long init(void) 17.??????????????????? { 18.??????????????????? ????char c; 19.??????????????????? ????long k=0; 20.??????????????????? ????c=getchar(); 21.??????????????????? ????while (!((c>='0')&&(c<='9'))){c=getchar();} 22.??????????????????? ????while ((c>='0')&&(c<='9')){ 23.??????????????????? ????????k=k*10+c-'0'; 24.??????????????????? ????????c=getchar(); 25.??????????????????? ????} 26.??????????????????? ????return k; 27.??????????????????? } 28.??????????????????? ? 29.??????????????????? void build(long x,long y,long len,long pn,long light) 30.??????????????????? { 31.??????????????????? ????tot++; 32.??????????????????? ????e[tot].a=x;e[tot].b=y;w[tot][0]=len;w[tot][1]=pn;w[tot][2]=light; 33.??????????????????? ????next[tot]=pos[x]; 34.??????????????????? ????pos[x]=tot; 35.??????????????????? } 36.??????????????????? ? 37.??????????????????? void spfa(long s,long t,long QU) 38.??????????????????? { 39.??????????????????? ????queue<long> q; 40.??????????????????? ????memset(v,sizeof(v),0); 41.??????????????????? ????for (long i=1;i<=n;i++)d[i]=1000000000; 42.??????????????????? ????d[s]=0;v[s]=true;q.push(s); 43.??????????????????? ????while (!q.empty()) 44.??????????????????? ????{ 45.??????????????????? ????????long now=q.front();q.pop(); 46.??????????????????? ????????long k=pos[now]; 47.??????????????????? ????????while (k) 48.??????????????????? ????????{ 49.??????????????????? ????????????long y=e[k].b; 50.??????????????????? ????????????if (d[y]>d[now]+w[k][QU]) 51.??????????????????? ????????????{ 52.??????????????????? ????????????????d[y]=d[now]+w[k][QU]; 53.??????????????????? ????????????????if (!v[y]) 54.??????????????????? ????????????????{ 55.??????????????????? ????????????????????q.push(y);v[y]=true; 56.??????????????????? ????????????????} 57.??????????????????? ? 58.??????????????????? ????????????} 59.??????????????????? ????????????k=next[k]; 60.??????????????????? ????????} 61.??????????????????? ????????v[now]=false; 62.??????????????????? ????} 63.??????????????????? ????printf("%ld",d[t]); 64.??????????????????? ????if (QU!=2)cout<<' '; else cout<<endl; 65.??????????????????? ? 66.??????????????????? } 67.??????????????????? ? 68.??????????????????? int main() 69.??????????????????? { 70.??????????????????? ? 71.??????????????????? ? 72.??????????????????? ??cin>>tt; 73.??????????????????? ????while(tt--){ 74.??????????????????? ????????cin>>n>>m>>s>>t; 75.??????????????????? ????????tot=0; 76.??????????????????? ????????memset(pos,0,sizeof(pos)); 77.??????????????????? ????????for (long i=0;i<m;i++) 78.??????????????????? ????????{ 79.??????????????????? ????????????char s; 80.??????????????????? ????????????s=getchar(); while (s!=',') s=getchar(); 81.??????????????????? ????????????long x=init();//cout<<x<<endl; 82.??????????????????? ????????????long y=init();//cout<<y<<endl; 83.??????????????????? ????????????long len=init();//cout<<len<<endl; 84.??????????????????? ????????????long pn=init();//cout<<pn<<endl; 85.??????????????????? ????????????long light=init();//cout<<light<<endl; 86.??????????????????? ????????????build(x,y,len,pn,light); 87.??????????????????? ????????????build(y,x,len,pn,light); 88.??????????????????? ????????} 89.??????????????????? ????????spfa(s,t,0); 90.??????????????????? ????????spfa(s,t,1); 91.??????????????????? ????????spfa(s,t,2); 92.??????????????????? ????} 93.??????????????????? ????return 0; 94.??????????????????? }
轉(zhuǎn)載于:https://www.cnblogs.com/USTC-ACM/archive/2013/03/11/2954398.html
總結(jié)
以上是生活随笔為你收集整理的上交三月月赛[SJTU] 1105 path的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何有效地读书【转】
- 下一篇: 字符串匹配自动机