[U]2.4.4 Bessie Come Home 简单最短路问题
来源:互联网 发布:置乱算法 arnold c语言 编辑:程序博客网 时间:2024/06/05 00:26
很简单的最短路问题,其中的关键就在于有些农场是没有牛的,这些农场可以走,但不能算在最终答案中,毕竟题中需要的是牛的最短路径。将'Z'点作为源点,进行一次赤裸裸的单源点最短路径,就可以得出答案了。代码中我用一个hash函数把所有的字母下标点都规整到[1,52]这个范围内。
/*ID:sevenst4LANG:C++PROG:comehome*/#include<stdio.h>#define INF 0x0FFFFFFFusing namespace std;int map[53][53];int f( char a ){ if( a>='A' && a<='Z' ) return a-'A'+1; else return a-'a'+1+26;}void dij(){ bool vis[53]; for( int i=0;i<53;i++ ) vis[i]=false; int p=f('Z'); vis[p]=true; for( int i=1;i<53;i++ ) { int min=INF; int index=-1; for( int j=1;j<53;j++ ) if( min>map[p][j] && vis[j]==false ) min=map[p][j],index=j; if( index==-1 )return ; vis[index]=true; for( int j=1;j<53;j++ ) { if( map[p][j]>map[p][index]+map[index][j] ) map[p][j]=map[p][index]+map[index][j]; } } return ;}int main(){ freopen( "comehome.in","r",stdin ); freopen( "comehome.out","w",stdout ); int n; for( int i=0;i<53;i++ ) for( int j=0;j<53;j++ ) map[i][j]=INF; scanf( "%d",&n ); for( int i=1;i<=n;i++ ) { char a,b,c;int len; scanf( "%c",&c ); scanf( "%c %c %d",&a,&b,&len ); if( map[f(a)][f(b)]>len ) map[f(a)][f(b)]=map[f(b)][f(a)]=len;}dij();int min=INF;int index;for( int i=1;i<26;i++ ) if( min>map[f('Z')][i] ) { min=map[f('Z')][i]; index=i; } printf( "%c %d\n",char(index+'A'-1),min ); return 0;}好久没写代码了... Dij都忘记了...
- [U]2.4.4 Bessie Come Home 简单最短路问题
- USACO section 2.4 Bessie Come Home(最短路)
- USACO-Section 2.4 Bessie Come Home(最短路[Dijkstra])
- USACO 2.4.4 Bessie Come Home
- usaco 2.4.4 Bessie Come Home
- USACO--2.4Bessie Come Home
- usaco 2.4 bessie come home
- USACO 2.4 Bessie Come Home (Floyd最短路径)
- 【USACO题库】2.4.4 Bessie Come Home回家
- JZOJ1274.【USACO题库】2.4.4 Bessie Come Home回家
- USACO 2.4 Bessie Come Home (comehome)
- usaco 2.4 PROB Bessie Come Home<水>
- USACO 2.4 D Bessie Come Home 题解
- [USACO2.4]回家 Bessie Come Home
- [usaco]Bessie Come Home
- bessie come home
- usaco Bessie Come Home
- Bessie Come Home
- 用wscript 操作 Excel 实现 SQL 排序
- Python:操作嵌入式数据库SQLite
- 用python for s60发送中文短信
- 电视看魔术vs雄鹿,电脑看尼克斯vs森林狼。眼花缭乱。
- 最有效提升网站排名的六种SEO手段
- [U]2.4.4 Bessie Come Home 简单最短路问题
- iPhone不可超越的6大优势
- linux下machine_desc结构体中的phys_io与io_pg_offst 的作用及使用方法
- 二维数组指针理解(论坛转载)
- Asp.net DataPager 分页点两次的问题
- [U]2.4.5 Fractions to Decimals 简单的语言控制题
- 打造属于自己的uboot
- C++中对sprintf()函数的说明
- [U]3.1.1 Agri-Net 简单的最小生成树问题Ford