HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
来源:互联网 发布:代源码百度云 编辑:程序博客网 时间:2024/06/02 16:40
最短路+字符串处理,,,
这题太迷惑人了,给的n可以那么大,其实图中的顶点数还不到200,因此floyd就能过,但是用G++很可能会超时,因此需要加上外挂。。另外需要注意的是起点和终点一样的情况。
floyd:
#include<iostream>#include<string.h>#include<string>#include<map>#include<algorithm>#define N 155#define MAX 99999999using namespace std;int Map[N][N];map<string,int>Q;int main(){int n;while(~scanf("%d",&n)){if(n==-1) break;for(int i=0;i<N;++i)for(int j=0;j<N;++j)Map[i][j]=(i==j)?0:MAX;string s,s1;cin>>s>>s1;Q.clear();int k=0,a;bool flag=false;map<string,int>::const_iterator it;Q.insert(pair<string,int>(s,++k));if(s1!=s) Q.insert(pair<string,int>(s1,++k));else flag=true;for(int i=0;i!=n;++i){cin>>s>>s1>>a;if(!flag) {int b,c;it=Q.find(s);if(it!=Q.end()) b=it->second;else Q.insert(pair<string,int>(s,++k)),b=k;it=Q.find(s1);if(it!=Q.end()) c=it->second;else Q.insert(pair<string,int>(s1,++k)),c=k;if(Map[b][c]>a) Map[b][c]=Map[c][b]=a;}}if(flag) {printf("0\n");continue;}for(int i=1;i<=k;++i)for(int j=1;j<=k;++j)for(int t=1;t<=k;++t)if(Map[j][i]!=MAX&&Map[i][t]!=MAX&&Map[j][i]+Map[i][t]<Map[j][t])Map[j][t]=Map[j][i]+Map[i][t]; if(Map[1][2]!=MAX) printf("%d\n",Map[1][2]); else printf("-1\n");}return 0;}
dijstra+优先队列:
#include<iostream>#include<string.h>#include<cstdio>#include<string>#include<map>#include<algorithm>#include<queue>#define N 160#define M 40005#define MAX 99999999using namespace std;typedef pair<int,int>pii;map<string,int>Map;void in(int &a){ char ch; while((ch=getchar())<'0'||ch>'9'); for(a=0;ch>='0'&&ch<='9';ch=getchar()) a=a*10+ch-'0';}typedef struct node{ int num; int len; int Next; friend bool operator<(node a,node b) {return a.len>b.len;}}Node;Node s[M];int head[N],res,dis[N],ans,n;int MAP[N][N];void init(){ res=0,ans=0; memset(head,-1,sizeof(head)); for(int i=0;i<N;++i) for(int j=0;j<N;++j) MAP[i][j]=(i==j)?0:MAX;}void add(int a,int b,int c){ s[res].num=b,s[res].len=c,s[res].Next=head[a],head[a]=res++;}void Djistra(){ priority_queue<pii,vector<pii>,greater<pii> >Q; for(int i=1;i<=ans;++i) dis[i]=(i==1)?0:MAX; Q.push(make_pair(dis[1],1)); while(!Q.empty()) { pii u=Q.top(); Q.pop(); int v=u.second; if(u.first!=dis[v]) continue; for(int i=head[v];i!=-1;i=s[i].Next) { if(dis[s[i].num]>dis[v]+s[i].len) { dis[s[i].num]=dis[v]+s[i].len; Q.push(make_pair(dis[s[i].num],s[i].num)); } } }}int main(){ while(~scanf("%d",&n)) { if(n==-1) break; init(); Map.clear(); string s,s1; cin>>s>>s1; bool flag=false; Map.insert(pair<string,int>(s,++ans)); if(s!=s1) Map.insert(pair<string,int>(s1,++ans)); else flag=true; map<string,int>::const_iterator it; for(int i=0;i!=n;++i) { string s,s1; int a,b,c; cin>>s>>s1; in(c); if(!flag) { it=Map.find(s); if(it!=Map.end()) a=it->second; else Map.insert(pair<string,int>(s,++ans)),a=ans; it=Map.find(s1); if(it!=Map.end()) b=it->second; else Map.insert(pair<string,int>(s1,++ans)),b=ans; if(MAP[a][b]>c) MAP[a][b]=MAP[b][a]=c; } } if(flag) {printf("0\n");continue;} for(int i=1;i<=ans;++i) for(int j=1;j<=ans;++j) if(i!=j) { if(MAP[i][j]<MAX) add(i,j,MAP[i][j]); } Djistra(); if(dis[2]==MAX) printf("-1\n"); else printf("%d\n",dis[2]); }return 0;}
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=1005
- http://acm.hdu.edu.cn/showproblem.php?pid=1333
- http://acm.hdu.edu.cn/showproblem.php?pid=2710
- http://acm.hdu.edu.cn/showproblem.php?pid=2866
- http://acm.hdu.edu.cn/showproblem.php?pid=1041
- http://acm.hdu.edu.cn/showproblem.php?pid=1719
- http://acm.hdu.edu.cn/showproblem.php?pid=2136
- http://acm.hdu.edu.cn/showproblem.php?pid=1166
- http://acm.hdu.edu.cn/showproblem.php?pid=3519
- http://acm.hdu.edu.cn/showproblem.php?pid=2446
- http://acm.hdu.edu.cn/showproblem.php?pid=1709
- http://acm.hdu.edu.cn/showproblem.php?pid=1874
- http://acm.hdu.edu.cn/showproblem.php?pid=1863
- http://acm.hdu.edu.cn/showproblem.php?pid=1879
- http://acm.hdu.edu.cn/showproblem.php?pid=1875
- http://acm.hdu.edu.cn/showproblem.php?pid=1102
- 我分析Flex的未来
- 透析设计模式中的 策略模式
- sencha 2.0 基于HTML5的sessionstorage的使用
- s3c2410fb_map_video_memory
- daemon函数实现原理
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- android IPC及原理简介
- 重载 overload
- 《软件测试与持续质量改进(第三版)》 读书心得
- 用命令提示符创建一个在windows非法的文件夹
- openCv学习笔记(一)——数字图形的基础和结构
- Android进程间通信(IPC)机制Binder介绍
- C++中sizeof与strlen
- Android Timer类的使用