标签:des style blog http io ar color os sp
Description
Input
Output
Sample Input
3 0 1 2 1 0 3 2 3 0 3 1 1 2 0
Sample Output
4
#include<iostream> #include<algorithm> using namespace std; int main() { int n; int a[50][50],b[7]; while(cin>>n) { int sum=999999; int t; if(n==0) break; int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>a[i][j]; int k; cin>>k; for(i=0;i<k;i++) cin>>b[i]; sort(b,b+k); for(i=0;i<k-1;i++) { if(b[i]==b[i+1]) { for(j=i;j<k;j++) b[j]=b[j+1]; k--; i--; } } t=a[0][b[0]]; for(i=1;i<k;i++) t+=a[b[i-1]][b[i]]; while(next_permutation(b,b+k)) { if(t<sum) sum=t; t=a[0][b[0]]; for(i=1;i<k;i++) t+=a[b[i-1]][b[i]]; } cout<<sum<<endl; } return 0; }
<img src="http://img.blog.csdn.net/20141204214530360?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGh5dG4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
#include<iostream> #include<algorithm> using namespace std; int main() { int n; while(cin>>n) { if(n==0) break; int a[50][50]; int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>a[i][j]; int k; cin>>k; int b[10]; for(i=0;i<k;i++) cin>>b[i]; sort(b,b+k); for(i=0;i<k-1;i++) { if(b[i]==b[i+1]) { for(j=i;j<k;j++) b[j]=b[j+1]; i--; k--; } } int t,sum=999999; do { t=a[0][b[0]]; for(i=1;i<k;i++) t+=a[b[i-1]][b[i]]; if(t<sum) sum=t; }while(next_permutation(b,b+k)); cout<<sum<<endl; } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/phytn/article/details/41730681