1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <iostream> #include <vector>
using namespace std;
int minnum(int a,int b,int c){ int n=a; if(a>b){ n=b; } if(n>c){ n=c; } return n; }
int maxinarr(int a[], int size){ int n=a[0]; for(int i=1;i<size;i++){ if(n<a[i]) n=a[i]; } return n; }
int mininarr(int a[], int size){ int n=a[0]; for(int i=1;i<size;i++){ if(n>a[i]) n=a[i]; } return n; }
int main(){ vector<int> startpoint; vector<int> endpoint; vector<int> weight; int number; cout << "请依次输入每段路经的起始点,结束点和权值(输入-1结束输入ps:-1所在行数字无效):" << endl; while (1) {
cin >> number; if (number == -1) break; startpoint.push_back(number); cin >> number; if (number == -1) break; endpoint.push_back(number); cin >> number; if (number == -1) break; weight.push_back(number); } int *sp = new int[startpoint.size()]; if (!startpoint.empty()){ memcpy(sp, &startpoint[0], startpoint.size()*sizeof(int)); } int *ep = new int[endpoint.size()]; if (!endpoint.empty()){ memcpy(ep, &endpoint[0], endpoint.size()*sizeof(int)); } int *wt = new int[weight.size()]; if (!weight.empty()){ memcpy(wt, &weight[0], weight.size()*sizeof(int)); } int size=minnum(startpoint.size(),endpoint.size(),weight.size());
int max=maxinarr(ep,size); if(maxinarr(sp,size)>maxinarr(ep,size)) int max=maxinarr(sp,size);
int sheet[max+1][max+1]; int min=mininarr(ep,size); if(mininarr(sp,size)>mininarr(ep,size)) int min=mininarr(sp,size); for(int i=min;i<=max;i++){ for(int j=min;j<=max;j++){ sheet[i][j]=99999; } } for(int i=0;i<size;i++){ if(sheet[sp[i]][ep[i]]>wt[i]){ sheet[sp[i]][ep[i]]=wt[i]; } }
cout<<"排序前:"<< endl; for(int i=min;i<=max;i++){ for(int j=min;j<=max;j++){ cout << sheet[i][j]<<" "; } cout << endl; } for(int k=min;k<=max;k++){ for(int i=min;i<=max;i++){ for(int j=min;j<=max;j++){ if(sheet[i][j]>sheet[i][k]+sheet[k][j]) sheet[i][j]=sheet[i][k]+sheet[k][j]; } } } cout<<"排序后:"<< endl; for(int i=min;i<=max;i++){ for(int j=min;j<=max;j++){ cout << sheet[i][j]<<" "; } cout << endl; } cout << endl; cout << " "; for(int i=min;i<=max;i++){ printf("%10d",i); } cout << endl; for(int i=min;i<=max;i++){ for(int j=min-1;j<=max;j++){ if(j==min-1){ printf("%10d",i); continue; } printf("%10d",sheet[i][j]); } cout << endl; } }
|