标签:
今天主要学习了图的有关内容,以及DFS,BFS,最短路问题的大致讲解,做了4道习题,完成了今日任务。
最短路的三种算法:
1.Dijkstra算法(使用连接矩阵,打起来简单,但是复杂度高)
2.Bellman Ford算法(松弛操作, 使用较少)
3.SFPA算法(第一种算法的优化,使用最多)
另外,求两个点之间的最短路:Floyd算法
今天做的4道题:
Description
Input
Output
Sample Input
Sample Output
我的代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<iomanip> #include<queue> using namespace std; typedef long long ll; const int N =110 ; const int INF = 10e7; int cost[N][N] ; int main() { int n, m; while(cin>>n>>m && ( n!=0 || m!= 0)){ for( int i = 0 ; i< N ; ++i) for( int j = 0 ; j< N; ++j) cost[i][j] = 0; for( int i = 1 ; i<=m ; ++i){ int x, y; cin>>x>>y; cin>>cost[x][y]; cost[y][x] = cost[x][y]; } ll lowest[N]; int flag[N]; for( int i= 1 ; i<N; ++i){ lowest[i] = INF ; flag[i] = 0; }// initialize lowest[1] = 0 ; for( int j= 1 ; j <=n ; ++j){ int min = INF; int k = -1; for( int i = 1; i<=n; ++i){ if( !flag[i] && lowest[i] < min ){ min = lowest[i] ; k = i; } } if( k==-1 ) break; flag[k] = 1; for( int i = 1; i<=n; ++i){ if( !flag[i] && cost[k][i]>0 && cost[k][i] + lowest[k] < lowest[i] ){ lowest[i] = cost[k][i] + lowest[k]; } } } cout<<lowest[n]<<endl; } return 0; }
用的第一种算法。
Description
Input
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
#include<iostream> #include<queue> #include<cstdio> #include<vector> using namespace std; const int MAXN = 1010; const int INF = 10e7; struct Edge{ int v, cost; Edge( int _v, int _cost) : v(_v), cost(_cost){} }; vector<Edge> E[MAXN]; void addEdge ( int u, int v, int cost){ E[u].push_back(Edge(v,cost)); } int main() { int T, N; cin>>T>>N; for( int i = 0 ; i< T; ++i){ int a, b, c; cin>>a>>b>>c; addEdge(a, b, c); addEdge(b, a, c); } //***********INITIALIZE**************// int lowest[MAXN], flag[MAXN]; queue<int> q; for( int i = 1; i<=N; ++i){ lowest[i] = INF; flag[i] = 0; } lowest[N]= 0 ; q.push(N) ; flag[N] = 1; //************MAIN*****************// while( !q.empty() ){ int x = q.front() ; q.pop(); flag [x] = 0; int len = E[x].size(); for( int i = 0 ; i<len ; ++i){ int v1= E[x][i].v; if( E[x][i].cost> 0 &&lowest[x] + E[x][i].cost < lowest[v1]){ lowest[ v1 ] = lowest[x] + E[x][i].cost ; if( !flag[v1] ){ flag[v1] = 1; q.push(v1); } } } } cout<<lowest[1]<<endl; return 0; }
用的第三种算法。
Description
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
我的代码
#include<iostream> #include<cstdio> #include<algorithm> #include<iomanip> #include<queue> #include<stack> #include<cmath> using namespace std; const int MAXN = 210 ; struct Point{ double x, y; }pt[MAXN]; double Max ( double a , double b){ return a>b?a:b; } double dis[MAXN][MAXN]; int main() { int n; int Case = 0; while( cin>>n && n!= 0 ){ Case ++; for( int i = 1 ; i <= n; ++i){ cin>>pt[i].x>>pt[i].y; } for( int i = 1; i<= n ; ++i) for( int j= 1 ; j<= n ; ++j) dis[i][j] = sqrt( (pt[i].x - pt[j].x)*(pt[i].x - pt[j].x) + (pt[i].y - pt[j].y)*(pt[i].y - pt[j].y)) ; for( int k = 1; k<= n; ++k) for( int i = 1; i<= n; ++i) for( int j = 1; j<= n; ++j){ if( dis[i][j] > dis[i][k] && dis[i][j] > dis[k][j] ) dis[i][j] = Max( dis[i][k] , dis[k][j]); } cout<<"Scenario #" <<Case<<endl <<"Frog Distance = "<<fixed<<setprecision(3)<<dis[1][2]<<endl; cout<<endl; } return 0 ; }
用了FLORD算法
Description
Input
Output
Sample Input
Sample Output
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<cmath> #include<vector> using namespace std; const int MAXN = 2010; const int INF = 10e7; int flag [MAXN]; int lowest[MAXN]; int N, A, B; bool valid ( int x ){ if( x >=1 && x<= N) return true; else return false; } int main(){ while( cin>>N && N!= 0){ cin>>A>>B; //vector <int> arr; int arr[MAXN]; //arr.push_back(0); queue <int> q; for(int i = 0 ; i<MAXN ; ++i){ arr[i] = 0; } for(int i = 1 ; i<= N ; ++i){ cin>>arr[i]; } // for( int i = 1 ; i<=N ;++i){ // int a; //cin>>a; // arr.push_back(a); // } for( int i = 0 ; i<= MAXN ; ++i){ flag [i] = 0 ; lowest[i]= INF; } //initialize flag[A] = 1 ; lowest[A] = 0; q.push(A); while( !q.empty()){ int x = q.front() ; q.pop(); flag[x] = 0 ; if( valid( x+arr[x]) && lowest[x] + 1 < lowest[ x+arr[x]] ){ lowest[x + arr[x]] = lowest[x] +1; if( flag[x+arr[x]] == 0){ flag[x + arr[x]] = 1; q.push(x +arr[x]) ; } } if( valid( x-arr[x]) && lowest[x] + 1 < lowest[ x-arr[x]] ){ lowest[x - arr[x]] = lowest[x] +1; if( flag[x-arr[x]] == 0){ flag[x - arr[x]] = 1; q.push(x -arr[x]) ; } } } if( lowest[B] != INF) cout<< lowest[B]<<endl; else cout<<-1<<endl; } return 0; }
用了第三种算法
今天觉的很有收获,继续加油!!!
标签:
原文地址:http://www.cnblogs.com/topW2W/p/5152673.html