//Tadrion #include <cstdio> #include <vector> #include <iostream> #include <deque> #include <map> #include <set> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <algorithm> #include <utility> using namespace std; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define CLEAR(x) (memset(x,0,sizeof(x))) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define VAR(v, n) __typeof(n) v = (n) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define DBG(v) cout<<#v<<" = "<<v<<endl; #define IN(x,y) ((y).find(x)!=(y).end()) #define ST first #define ND second #define PB push_back #define PF push_front #define MP make_pair typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MAXXX 100010 #define INF 100000000000000010LL LL n,k; vector<pair<LL,LL> > v[MAXXX]; LL dj[MAXXX],p[MAXXX],res[MAXXX]; LL a,b,c,d; LL dijkstra(LL s) { deque<pair<LL,LL> > r; //printf("D1\n"); FOR(i,1,n) dj[i]=INF; dj[s]=0; r.PB(MP(dj[s],s)); //printf("D2\n"); while(!r.empty()) { pair<LL,LL> it = r.front(); LL cur = it.ST; LL nod = it.ND; r.pop_front(); REP(i,SZ(v[nod])) { if(dj[v[nod][i].ST] > cur+v[nod][i].ND) { dj[v[nod][i].ST] = cur+v[nod][i].ND; r.PB(MP(dj[v[nod][i].ST],v[nod][i].ST)); } } } //printf("D3\n"); LL best,bestp; if(s==1) {best=2;bestp = p[2]-dj[2];} else {best=1; bestp = p[1]-dj[1];} FOR(i,1,n) { if(i!=s) { res[i] = p[i]-dj[i]; if(res[i] > bestp) { best = i; bestp = res[i]; } } } //printf("D4\n"); return best; } int main() { cin >> n >> k; FOR(i,1,n) cin >> p[i]; FOR(i,1,n-1) { cin >> a >> b >> c; v[a].PB(MP(b,c)); v[b].PB(MP(a,c)); } LL last = 1; LL last2; FOR(i,1,k) { cin >> a; if(1==a) { cin >> b >> c; //cout << "b: " << b << " c: " << c << "\n"; p[b]=c; } else { cin >> a >> b >> c; //cout << "d: " << d << " b: " << b << " c: " << c << "\n"; REP(i,SZ(v[a])) { if(v[a][i].ST == b) { v[a][i] = MP(b,c); break; } } REP(i,SZ(v[b])) { if(v[b][i].ST == a) { v[b][i] = MP(a,c); break; } } } last2 = dijkstra(last); last = last2; cout << last << " "; } cout << "\n"; return 0; }
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | //Tadrion #include <cstdio> #include <vector> #include <iostream> #include <deque> #include <map> #include <set> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <algorithm> #include <utility> using namespace std; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define CLEAR(x) (memset(x,0,sizeof(x))) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define VAR(v, n) __typeof(n) v = (n) #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define DBG(v) cout<<#v<<" = "<<v<<endl; #define IN(x,y) ((y).find(x)!=(y).end()) #define ST first #define ND second #define PB push_back #define PF push_front #define MP make_pair typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MAXXX 100010 #define INF 100000000000000010LL LL n,k; vector<pair<LL,LL> > v[MAXXX]; LL dj[MAXXX],p[MAXXX],res[MAXXX]; LL a,b,c,d; LL dijkstra(LL s) { deque<pair<LL,LL> > r; //printf("D1\n"); FOR(i,1,n) dj[i]=INF; dj[s]=0; r.PB(MP(dj[s],s)); //printf("D2\n"); while(!r.empty()) { pair<LL,LL> it = r.front(); LL cur = it.ST; LL nod = it.ND; r.pop_front(); REP(i,SZ(v[nod])) { if(dj[v[nod][i].ST] > cur+v[nod][i].ND) { dj[v[nod][i].ST] = cur+v[nod][i].ND; r.PB(MP(dj[v[nod][i].ST],v[nod][i].ST)); } } } //printf("D3\n"); LL best,bestp; if(s==1) {best=2;bestp = p[2]-dj[2];} else {best=1; bestp = p[1]-dj[1];} FOR(i,1,n) { if(i!=s) { res[i] = p[i]-dj[i]; if(res[i] > bestp) { best = i; bestp = res[i]; } } } //printf("D4\n"); return best; } int main() { cin >> n >> k; FOR(i,1,n) cin >> p[i]; FOR(i,1,n-1) { cin >> a >> b >> c; v[a].PB(MP(b,c)); v[b].PB(MP(a,c)); } LL last = 1; LL last2; FOR(i,1,k) { cin >> a; if(1==a) { cin >> b >> c; //cout << "b: " << b << " c: " << c << "\n"; p[b]=c; } else { cin >> a >> b >> c; //cout << "d: " << d << " b: " << b << " c: " << c << "\n"; REP(i,SZ(v[a])) { if(v[a][i].ST == b) { v[a][i] = MP(b,c); break; } } REP(i,SZ(v[b])) { if(v[b][i].ST == a) { v[b][i] = MP(a,c); break; } } } last2 = dijkstra(last); last = last2; cout << last << " "; } cout << "\n"; return 0; } |