Untitled
unknown
plain_text
2 years ago
1.8 kB
3
Indexable
#include<iostream> using namespace std; // mentor 2 100 int const max_size = 105; int const vc = 1000000; int mayBD[max_size]; int A[max_size][max_size]; int visit[max_size]; int n; // Số Thành int ans; bool flag; // Tìm được chu trình hay không void reset(){ // Reset de dfs for( int i = 0; i<n; i++) visit[i] = 0; } void dfs(int tempThanh, int thanh, int sl, int tempMin, int d1, int d2){ if ( tempThanh == thanh && sl >= 3){ flag = true; ans += tempMin; A[d1][d2] = 0; A[d2][d1] = 0; return; } for(int col =0; col< n; col++){ if (flag) return; if (A[tempThanh][col] == 1 && (!visit[col] || (col == thanh && sl >= 3))){ visit[col] = 1; int value = mayBD[tempThanh] + mayBD[col]; if( tempMin > value) { dfs(col,thanh,sl+1,value, tempThanh, col); } else{ dfs(col,thanh,sl+1,tempMin,d1,d2); } } } } void solve(){ for(int thanh = 0; thanh <n; thanh++){ // Duyệt các thành để dfs reset(); visit[thanh] = 1; flag = false; dfs(thanh, thanh, 1, vc, -1, -1); while(flag){ reset(); visit[thanh] = 1; flag = false; dfs(thanh, thanh, 1, vc, -1, -1); } } } int main(){ freopen("input1.txt", "r", stdin); int T; cin>>T; for ( int tc = 1; tc <= T; tc++) { cin>>n; for( int i = 0; i< n; i++){ for( int j = 0; j<n; j++){ A[i][j] = 0; // Reset matran } } int thanh, soMayBD,slKe, tempThanh; for (int i = 0; i<n; i++) { cin>>thanh>>soMayBD>>slKe; mayBD[thanh] = soMayBD; for(int k = 0; k< slKe; k++){ cin>>tempThanh; A[thanh][tempThanh] = 1; A[tempThanh][thanh] = 1; } } ans = 0; solve(); cout<<ans<<endl; } return 0; }
Editor is loading...