Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Un comis voiajor pleaca dintr-un oras, trebuie sa viziteze un nr de orase si se intoarce in orasul de unde a plecat cu efort minim.
- Orice oras "i" este legat printr-o sosea de orice oras "j" de lungime "a[i][j]" km. Se cere traseul pe care trebuie sa.l urmeze comis-voiajorul
- astfel incat sa parcurga astfel incat sa parcurga un numar minim de km.
- */
- #include<iostream>
- #include<fstream>
- using namespace std;
- ifstream f("orase.in");
- int a[50][50],n,i,j,o,s[50],p,pp,mini,distanta;
- void citire()
- {
- f>>n;
- for(i=1; i<=n; i++)
- for(j=1; j<=n; j++)
- f>>a[i][j];
- }
- int main()
- {
- citire();
- do
- {
- cout<<"Orasul de plecare ";
- cin>>o;
- }
- while(o<0 || o>n);
- s[o]=1;
- p=o;
- cout<<"p="<<p<<" ";
- for(i=1; i<n; i++)
- {
- mini=30000;
- for(j=1; j<=n; j++)
- if(s[j]==0 && a[j][p] && mini>a[j][p])
- {
- mini=a[j][p];
- pp=j;
- }
- s[pp]=1;
- distanta=distanta+a[pp][p];
- p=pp;
- cout<<pp<<" ";
- }
- cout<<'\n';
- cout<<"cost="<<distanta;
- return 0;
- }
- /* orase.in */
- 5
- 0 1 5 5 3
- 1 0 2 4 1
- 5 2 0 6 1
- 5 4 6 0 9
- 3 1 1 9 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement