unblocked Works continuedTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U
Description
A province has been building a lot of roads since the implementation of many years of smooth engineering projects. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.
Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.
Input
This topic contains multiple sets of data, please handle to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.
Output
For each set of data, output the shortest distance to walk in a row. If there is no route from S to T, then output-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
Floyd algorithm:
This algorithm is easier to understand, but the data is too big to be done.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1typedef Long long ll;int map[205][205];int n,m;int l LSS (int s,int e) {for (int z=0;z<n;z++) for (int. i=0;i<n;i++) for (int j=0;j<n;j++) { if (Map[i][j]>map[i][z]+map[z][j]) map[i][j]=map[i][z]+map[z][j]; } if (Map[s][e]==inf) return-1; else return map[s][e];} int main () {//fin int st,ed,cost; int s,e; while (~SCANF ("%d%d", &n,&m)) {memset (map,inf,sizeof (MAP)); for (int i=0;i<n;i++) {map[i][i]=0; } for (int i=1;i<=m;i++) { scanf ("%d%d%d", &st,&ed,&cost); Map[ed][st]=map[st][ed]=min (Map[st][ed],cost); } scanf ("%d%d", &s,&e); int ANS=LLSS (S,E); printf ("%d\n", ans); }}
Dijkstra algorithm:
This reduces the complexity of time
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1typedef long long ll;const int MAXN=205;int Map[MAXN ][maxn];int dis[maxn];int vis[maxn];int n,m;int llss (int s,int e) {for (int i=0;i<n;i++) {dis[i]=map[s][i]; vis[i]=0; } vis[s]=1; dis[s]=0; int temp,k; for (int i=0;i<n;i++) {temp=inf; for (int j=0;j<n;j++) {if (Vis[j]==0&&temp>dis[j]) {k=j; TEMP=DIS[J]; }} if (Temp==inf) break; Vis[k]=1; for (int j=0;j<n;j++) {if (Dis[j]>dis[k]+map[k][j]) dis[j]=dis[k]+map[k][j]; }} if (Dis[e]==inf) return-1; else return dis[e];} int main () {//fin int st,ed,cost; int s,e; while (~SCANF ("%d%d", &n,&m)) {memset (map,inf,sizeof (MAP)); for (int i=0;i<n;i++) {map[i][i]=0; } for (int i=1;i<=m;i++) {scanf ("%d%d%d", &st,&ed,&cost); Map[ed][st]=map[st][ed]=min (Map[st][ed],cost); } scanf ("%d%d", &s,&e); int ANS=LLSS (S,E); printf ("%d\n", ans); }}
HDU 1874 unblocked project continued shortest circuit