图论模型与算法

UVA 1395

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<cstdio>
using namespace std;

const int Max = 100+15, inf = 9999999;
int N, K;
int fa[Max];
int cnt;
struct Edge{
	int a, b, v;
	bool operator < (const Edge & rhs) const{
	   return v < rhs.v;
	}
};

vector<Edge> V;

void init(){
	for(int i = 1; i <= N; i++) fa[i] = i;
    cnt = 0; 
}

int findfa(int x)  {while(x != fa[x]) x = fa[x]; return x;}

int main(){
   while(scanf("%d%d",&N, &K) == 2 && N){
   	   V.clear();
   	   for(int i = 0; i < K; i++){
   	   	   int a,b,v; 
   	   	   scanf("%d%d%d",&a,&b,&v);
   	   	   V.push_back( Edge{a,b,v} );
   	   }
   	   sort(V.begin(),V.end());
   	   int ans = inf;
   	   for(int L = 0; L < K; L++){
   	   	   init();
		   for(int R = L; R < K; R++){
   	   	   	   int u = findfa( V[R].a ), v = findfa( V[R].b );
   	   	   	   if( u != v) { 
					fa[u] = v;
					if(++cnt == N-1) { ans = min(ans, V[R].v - V[L].v);  break;}
			   }
   	   	   }
   	   }
	   if(ans == inf) cout<<"-1"<<endl;
   	   else cout<<ans<<endl;
   }
   return 0;
} 


/*
sample input

4 5
1 2 3
1 3 5
1 4 6
2 4 6
3 4 7

4 6
1 2 10
1 3 100
1 4 90
2 3 20
2 4 80
3 4 40

2 1
1 2 1

3 0

3 1
1 2 1

3 3
1 2 2
2 3 5
1 3 6

5 10
1 2 110
1 3 120
1 4 130
1 5 120
2 3 110
2 4 120
2 5 130
3 4 120
3 5 110
4 5 120

5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422

5 8
1 2 1
2 3 100
3 4 100
4 5 100
1 5 50
2 5 50
3 5 50
4 1 150

0 0

sample output

1 20 0 -1 -1 1 0 1686 50

*/
 
 

UVA 10048

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<cstdio>
using namespace std;

const int maxn = 100+5, inf = 100000+10;
int C, S, Q, kase = 0;
int a, b, v;
int d[maxn][maxn];

int main(){
	while(cin>>C>>S>>Q && C){
	   fill(d[0], d[0]+maxn*maxn, inf);
	   for(int i= 1; i <= C; i++) d[i][i] = 0;
	   for(int i = 0; i < S; i++){
	      cin>>a>>b>>v;
	      d[a][b] = d[b][a] = v;
	   }
	   for(int k = 1; k <= C; k++)
	      for(int i = 1; i <= C; i++)
	         for(int j = 1; j <= C; j++) if(d[i][k] < inf && d[k][j] < inf)
	            d[i][j] = min(d[i][j], max(d[i][k],d[k][j]) );
	         
	   if(kase > 0) putchar('\n');
	   cout<<"Case #"<<++kase<<endl;
	   while(Q--){
		  cin>>a>>b;
		  if(d[a][b] != inf) cout<<d[a][b]<<endl;
		  else cout<<"no path"<<endl;	
	   }
	}
	return 0;
}




/*
sample input

7 9 3
1 2 50 
1 3 60
2 4 120
2 5 90
3 6 50
4 6 80
4 7 70
5 7 40 
6 7 140
1 7
2 6
6 2

7 6 3
1 2 50
1 3 60
2 4 120
3 6 50
4 6 80
5 7 40
7 5
1 7
2 4

0 0 0

sample output

Case #1
80
60
60

Case #2
40
no path
80

*/

UVA 247

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<cstdio>
using namespace std;

const int maxn = 25+5;
int N, K, cnt, kase = 0;
string a, b;
int G[maxn][maxn];
map<string,int>IDcache;
map<int,string>NameCache;
int fa[maxn];

int ID(string a){
	if(!IDcache.count(a)) {IDcache[a] = cnt;  NameCache[cnt++] = a;} 
	return IDcache[a];
}

int findfa(int x){
	while(x != fa[x]) x = fa[x];
	return x;
}

void solve(){
	for(int k = 0; k < N; k++)
	   for(int i = 0; i < N; i++)
	      for(int j = 0; j < N; j++)
	         G[i][j] = ( G[i][j] || (G[i][k] && G[k][j]) );
	for(int i = 0; i < N; i++) 	{ G[i][i] = 1;  fa[i] = i;}         
	for(int i = 0; i < N; i++)
	   for(int j = i; j < N; j++) if(G[i][j] && G[j][i]){
	   	  int u = findfa(i), v = findfa(j);
	   	  if(u != v) fa[u] = v;
	   }
	vector<vector<int> >v(maxn);
	map<int,int>m;
	int k = 0;
	for(int i = 0; i < N; i++) {
		int u = findfa(i);
		if(!m.count(u))	m[u] = k++;
		v[ m[u] ].push_back(i);
	}
	if(kase > 0) printf("\n");
	printf("Calling circles for data set %d:\n",++kase);
	for(int i = 0; i < k; i++){
		for(int j = 0; j < v[i].size(); j++){
		    if(j) cout<<", ";
			cout<<NameCache[ v[i][j] ];
		}
		putchar('\n');
	}
	return;
}

int main(){
	while(cin>>N>>K && N){
	   memset(G,0,sizeof(G));
	   IDcache.clear();
	   NameCache.clear();
	   cnt = 0; 
	   for(int i = 0; i < K; i++){
	      cin>>a>>b;
	      G[ ID(a) ][ ID(b) ] = 1;
       }
       solve();
	}
	return 0;
}




/*
sample input

5 6
Ben Alexander
Alexander Dolly
Dolly Ben
Dolly Benedict
Benedict Dolly
Alexander Aaron
14 34
John Aaron
Aaron Benedict
Betsy John
Betsy Ringo
Ringo Dolly
Benedict Paul
John Betsy
John Aaron
Benedict George
Dolly Ringo
Paul Martha
George Ben
Alexander George
Betsy Ringo
Alexander Stephen
Martha Stephen
Benedict Alexander
Stephen Paul
Betsy Ringo
Quincy Martha
Ben Patrick
Betsy Ringo
Patrick Stephen
Paul Alexander
Patrick Ben
Stephen Quincy
Ringo Betsy
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Quincy Martha
0 0

sample output

Calling circles for data set 1:
Ben, Alexander, Dolly, Benedict
Aaron

Calling circles for data set 2:
John, Betsy, Ringo, Dolly
Aaron
Benedict
Paul, George, Martha, Ben, Alexander, Stephen, Quincy, Patrick

*/

UVA 658

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
using namespace std;

const int maxc = 20+5, maxn = 100+5, inf = 99999999;
typedef pair<int, int> pii;
int N, M;
int t[maxn], dist[1<<maxc];
bool v[1<<maxc];
string befor[maxn], after[maxn];

int solve(){
	for(int i = 0; i < (1<<N); i++) { dist[i] = inf;  v[i] = false;}
	dist[(1<<N)-1] = 0;  
	priority_queue<pii,vector<pii>, greater<pii> >Q;
    Q.push( make_pair(0, (1<<N)-1) );
	while(!Q.empty()){
		pii u = Q.top(); Q.pop();
	    int bugs = u.second;
	    if(!bugs) return u.first;
		if(v[bugs]) continue;
		v[bugs] = true;
	    for(int i = 0; i < M; i++){
	    	bool match = true;
	    	for(int j = 0; j < N; j++){
	    		if(befor[i][j] == '+' && !(bugs & (1<<j) )) {match = false; break;}
	    		if(befor[i][j] == '-' &&  (bugs & (1<<j) )) {match = false; break;}
	    	}
	    	if(!match) continue;
	    	int newbugs = bugs, newdist = u.first + t[i];
            for(int j = 0; j < N; j++) {
                if(after[i][j] == '-') newbugs &= ~(1<<j);
                if(after[i][j] == '+') newbugs |= (1<<j);
            }
	    	if( dist[newbugs] < -1 || newdist < dist[newbugs] ){
	    		 dist[newbugs] = newdist;		 
	             Q.push( make_pair(newdist, newbugs) );
			}
	    }
	}
	return -1;
}

int main(){
	int kase = 0;
	while(cin>>N>>M && N){
	    cout<<"Product "<<++kase<<endl;
	    for(int i = 0; i < M; i++) cin>>t[i]>>befor[i]>>after[i];
		int ans = solve();
		if(ans < 0) cout<<"Bugs cannot be fixed.\n\n";
		else cout<<"Fastest sequence takes "<<ans<<" seconds.\n\n";
	}
	return 0;
}



/*
sample input


3 3
1 000 00-
1 00- 0-+
2 0-- -++

4 1
7 0-0+ ---

0 0


sample output

Product 1
Fastest sequence takes 8 seconds.

Product 2
Bugs cannot be fixed.



*/


猜你喜欢

转载自blog.csdn.net/a874288174/article/details/80324166