int main() {
    int tc;
    cin >> tc;
    while (tc--) {
        int n, m;
        cin >> n >> m;
        AdjList.clear(); AdjList.resize(n);
        for (int i = 0; i < m; i++) {
            int a, b;
            cin >> a >> b;
            AdjList[a].push_back(ii(b, 0));
        }
        bool cac = true;
        int numScc = 0;
        dfs_num.clear(); dfs_num.resize(n, UNVISITED);
        dfs_parent.clear(); dfs_parent.resize(n, 0);
        for(int i = 0; i < n; i++) {
            if(dfs_num[i] == UNVISITED) {
                numScc++;
                if(graphCheck(i)) {
                    cac = false;
                    break;
                }
            }
        }
        if(cac && numScc == 1) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}
void bellmann_ford_extended(vvii &e, int source, vi &dist, vb &cyc) {
	dist.assign(e.size(), INF);
	cyc.assign(e.size(), false); // true when u is in a <0 cycle
	dist[source] = 0;
	for (int iter = 0; iter < e.size() - 1; ++iter){
		bool relax = false;
		for (int u = 0; u < e.size(); ++u)
			if (dist[u] == INF) continue;
			else for (auto &e : e[u])
				if(dist[u]+e.second < dist[e.first])
					dist[e.first] = dist[u]+e.second, relax = true;
		if(!relax) break;
	}
	bool ch = true;
	while (ch) {				// keep going untill no more changes
		ch = false;				// set dist to -INF when in cycle
		for (int u = 0; u < e.size(); ++u)
			if (dist[u] == INF) continue;
			else for (auto &e : e[u])
				if (dist[e.first] > dist[u] + e.second
					&& !cyc[e.first]) {
					dist[e.first] = -INF;
					ch = true;		//return true for cycle detection only
					cyc[e.first] = true;
				}
	}
}
void dijkstra_multiple_paths(vvii& g, int src, vi& dist, vvi& pred) {
    // initialize
    dist.assign(g.size(), numeric_limits<int>::max());
    dist[src] = 0;
    pred.assign(g.size(), vector<int>());
    set<ii> set = { { 0, src } };

    while (!set.empty()) {
        // extract min
        int u = set.begin()->second;
        set.erase(set.begin());

        for (auto& e : g[u]) {
            int v = e.first;
            int w = e.second;
            if (dist[u] + w < dist[v]) {
                // relax / decrease key
                set.erase({ dist[v], v });
                dist[v] = dist[u] + w;
                pred[v] = { u };
                set.insert({ dist[v], v });
            } else if(dist[u] + w == dist[v]) {
                pred[v].push_back(u);
            }
        }
    }
}
Example #4
0
int main() {
    ios::sync_with_stdio(0);
    int n;
    while(cin >> n && n) {
        graph.assign(n, vii());
        for(int i = 0; i < n - 1; i ++) {
            int a, b; cin >> a >> b;
            a--, b--;
            graph[a].push_back(ii(b, -1)), graph[b].push_back(ii(a, -1));
        }
        ways(n - 1, -1);
        ways(0, -1);
        vi dis(n, -1);
        bfs(0, dis);
        int l = 0, h = 3000, ans = 0;
        while(l <= h) {
            int mid = (l + h) / 2;
            if(check(n - 1, -1, dis, mid)) h = mid - 1, ans = mid;
            else l = mid + 1;
        }
        cout << ans << endl;
    }


    return 0;
}
int main(int argc, char const *argv[])
{
	int n,m,u,v,t,s,r;
	scanf("%d",&t);	
	while(t--){		
		for (int i = 0; i < 4000; ++i)
		{
			dist[i]= 500000;
		}
		memset(visited,0,sizeof(visited));		
		scanf("%d%d",&n,&m);
		adjList.assign(n+1, vii());
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d%d",&u,&v,&r);
			adjList[u].push_back(ii(v,r));
			adjList[v].push_back(ii(u,r));
		}		
		scanf("%d",&s);
		dijkastra(s);
		for (int i = 1; i <= n; ++i)
		{
			if(i!=s&&dist[i]==500000)
				printf("-1 ");
			else if(i!=s)
				printf("%d ", dist[i]);
		}	
		printf("\n");
	}
	return 0;
}
Example #6
0
vvii buildGraph(){
	int n;
	printf("no of vertices:");
	scanf("%d",&n);//no of vertices

	FILE* fp=fopen("weightedGraph.txt","r");

	vvii G(100);
	G.resize(n);
	
	/*printf("<G size:%d>",G.size());
	for(int l=0; l<n; l++)	
		printf("<%d>",G[l].size());*/
	int i,a,w;	
	do{
				
		fscanf(fp,"%d",&i);
		//printf("<%d>",i);
		if(i>=n) {printf("breaking because %d",i);break;}
		char c='a';	
		while(c!='\n'){	
			fscanf(fp,"%d",&a);
			fscanf(fp,"%d",&w);
			ii p;
			p.first=a; p.second=w;
			G[i].push_back(p);
			fscanf(fp,"%c",&c);
			//printf("%d %d %c",p.first,p.second,c);
			//debug();	
			}
				
		}while(i!=n-1);
		
return G;
}
int main() {
    ios::sync_with_stdio(0);
    int n, m, q, tc = 0;
    while(cin >> n >> m >> q && (m || n || q)) {
        if(tc++) cout << endl;
        buildUfds(n);
        edges.clear();
        for(int i = 0; i < m; i++) {
            int a, b, d; cin >> a >> b >> d;
            edges.push_back(make_pair(d, make_pair(a - 1, b - 1)));
        }
        sort(edges.begin(), edges.end());
        graph.clear(); graph.resize(n);
        kruskal();
        cout << "Case #" << tc << endl;
        vvi memo(n, vi(n, -1));
        for(int i = 0; i < q; i++) {
            int a, b; cin >> a >> b;
            a--; b--;
            if(memo[a][b] != -1) {
                if(memo[a][b] == -2) cout << "no path\n";
                else cout << memo[a][b] << endl;
            }
            else if(memo[b][a] != -1) {
                if(memo[b][a] == -2) cout << "no path\n";
                else cout << memo[b][a] << endl;
            }
            else {
                des = b; ans = -inf;
                vis.clear(); vis.resize(n, 0);
                dfs(a);
                if (ans != -inf) {
                    cout << ans << endl;
                    memo[a][b] = memo[b][a] = ans;
                }
                else {
                    cout << "no path\n";
                    memo[a][b] = memo[b][a] = -2;
                }
            }
        }
    }
    return 0;
}
int longest_increasing_subsequence(vvii &boxes, vi &lis, vi &dad) {
	for (int i = 0; i < boxes.size(); i++) {
		lis.push_back(1);
		dad.push_back(i);
	}

	int idx = 0;

	for (int i = 0; i < boxes.size(); i++) {
		for (int j = i - 1; j >= 0; j--) {
			if (nests(boxes[i].first, boxes[j].first) and lis[i] < lis[j] + 1) {
				lis[i] = lis[j] + 1;
				dad[i] = j;
				if (lis[i] > lis[idx]) idx = i;
			}
		}
	}

	return idx;
}
Example #9
0
void showGraph(vvii G){
	int n=G.size();
	for(int i=0; i<n; i++){
		int t=G[i].size();
		printf("<%d>:",i);	
		for(int j=0; j<t; j++){
			printf("Vertex:%d, Weight:%d",G[i][j].first,G[i][j].second);
		}
		printf("\n");
		}	
}
int main(){

    ios::sync_with_stdio(0);

    while(cin >> n && (n != -1)) {
        vec.clear();
        vec.resize(n);
        int enrgy, b, num;
        for (int i = 0; i < n; ++i) {
            cin >> enrgy >> num;
            for (int j = 0; j < num; ++j) {
                cin >> b;
                vec[i].push_back(ii(enrgy, b - 1));
            }
        }
        blmnfrd();
        vis.clear(); vis.resize(n , 0);
        dfs(0);
        vi vis1 = vis;

        if(dist[n - 1] > 0) cout << "winnable" << endl;
        else {
            bool happen = false;
            for (int i = 0; i < n && !happen; ++i)
                for (auto j : vec[i])
                    if (dist[j.second] < dist[i] + j.first && dist[i] + j.first > 0) {
                        vis.clear(); vis.resize(n,0);
                        dfs(i);
                        if (vis[n - 1] && vis1[i]) {
                            happen = true;
                            break;
                        }
                    }
            //if(chk())  cout << "winnable" << endl;
            if (happen) cout << "winnable" << endl;
            else cout << "hopeless\n";
        }
    }

    return 0;
}
Example #11
0
int main(){


    ios::sync_with_stdio(0);
    int door,w,a;
    while(cin >> n && (n!=-1)){
        adjlist.clear();
        adjlist.resize(n);
        vis.clear(); vis.resize(n,0);
        for (int i = 0; i < n; ++i) {
            cin >> w >> door;
            while(door--){
                cin >> a;
                adjlist[i].push_back(ii(a-1,w));
            }
        }
        bellmanFord();

        if(dist[n-1] > 0) cout << "winnable\n";
        else{
            bool haspos = false;
            for (int i = 0; i < n && !haspos; ++i) {
                for(auto & e: adjlist[i]){
                    if(dist[i] + e.second > 0 && dist[e.first] < dist[i] + e.second){
                        vis.clear(); vis.resize(n,0);
                        dfs(i);
                        if(vis[n-1] ){
                            haspos = true;
                            break;
                        }
                    }
                }
            }
            if(haspos) cout << "winnable\n";
            else cout << "hopeless\n";
        }
    }

    return 0;
}
int main(){

    ios::sync_with_stdio(0);

    while (cin >> n >> k){
        vi speed(n);
        vec.clear(); vec.resize(100);
        for (int i = 0; i < n; ++i)
            cin >> speed[i];
        string a , chert;
        getline(cin , chert);
        map <int , int > m;
        for ( int i = 0; i < n; i++) {
            getline(cin, a);
            stringstream str(a);
            int num;
            vi tmp;
            while (str >> num)
                tmp.push_back(num), m[num] = 1;
            for (int ii = 0; ii < tmp.size(); ii++) {
                for (int j = ii + 1; j < tmp.size() ; j++) {
                    vec[tmp[ii]].push_back(make_pair(speed[i] * (tmp[j] - tmp[ii]), tmp[j]));
                    vec[tmp[j]].push_back(make_pair(speed[i] * (tmp[j] - tmp[ii]), tmp[ii]));
                }
            }
        }
        int ans = dijkstra();
        if(ans != inf) {
            if (k != 0)
                ans -= 60;
            cout << ans << endl;
        }

        else  cout << "IMPOSSIBLE\n";

    }

    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    int tc; cin >> tc;
    while(tc--) {
        int n; cin >> n;
        fc.assign(n, pair<string, int>("", 0));
        for(auto &e: fc) cin >> e.first >> e.first >> e.second;

        n; cin >> n;
        sc.assign(n, pair<string, int>("", 0));
        for(auto &e: sc) cin >> e.first >> e.first >> e.second;

        memo.assign(fc.size() + 10, vii(sc.size() + 10, ii(-1, -1)));
        ii ans = solve(0, 0);
        if(ans.first == inf && ans.second == inf) cout << 0 << " " << 0 << endl;
        else cout << ans.first << " " << ans.second << endl;
    }


    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    int tc; cin >> tc;
    while(tc--) {
        scc.clear();
        int n, m; cin >> n >> m;
        AdjList.assign(n, vii());
        for(int i = 0; i < m; i++) {
            int a, b; cin >> a >> b;
            a--, b--;
            if(a != b)
            AdjList[a].push_back(ii(b, 0));
        }
        int V = n;

        dfs_num.assign(V, 0); dfs_low.assign(V, 0); visited.assign(V, 0); node_to_scc_num.assign(V, -1);
        dfsNumberCounter = numSCC = scc_num = 0;
        for (int i = 0; i < V; i++)
            if (dfs_num[i] == 0)
                tarjanSCC(i);

        graph.assign(scc_num, vi());
        for(int i = 0; i < scc.size(); i++) {
            for(int j = 0; j < scc[i].size(); j++) for(auto &e: AdjList[scc[i][j]])
                if(node_to_scc_num[e.first] != i) graph[i].push_back(node_to_scc_num[e.first]);
        }
        memo.assign(scc_num, -1);
        int ans = 0;
        for(int i = 0; i < scc_num; i++) {
            int cur = scc[i].size();
            ans = max(ans, solve(i) + cur);
        }
        cout << ans << endl;

    }


    return 0;
}
int main() {
    ios::sync_with_stdio(0);
    int n;
    while(cin >> n && n) {
        int m; cin >> m;
        AdjList.assign(n + 10, vii());
        for(int i = 0; i < m; i++) {
            int a, b, c; cin >> a >> b >> c;
            AdjList[a].push_back(ii(b, c));
            AdjList[b].push_back(ii(a, c));
        }

        int s = 2;
        dist.assign(n + 10, inf); dist[s] = 0;
        priority_queue<ii, vector<ii>, greater<ii> > pq;
        pq.push(ii(0, s));
        while (!pq.empty()) {
            ii front = pq.top(); pq.pop();
            int d = front.first, u = front.second;
            if (d > dist[u]) continue; // this is a very important check
            for (int j = 0; j < (int) AdjList[u].size(); j++) {
                ii v = AdjList[u][j];
                if (dist[u] + v.second < dist[v.first]) {
                    dist[v.first] = dist[u] + v.second;
                    pq.push(ii(dist[v.first], v.first));
                }
            }
        }

        memo.assign(n + 10, -1);
        cout << solve(1) << endl;


    }


    return 0;
}
void dijkstra(vvii &E, vi &dist, int s) {
	int N = E.size();
	dist.assign(N, LLINF);
	priority_queue<ii, vector<ii>, greater<ii>> pq;
	dist[s] = 0LL;
	pq.push({0LL, s});
	
	while (!pq.empty()) {
		ll d = pq.top().first;
		int u = pq.top().second;
		pq.pop();
		
		if (d != dist[u]) continue;
		for (ii vw : E[u]) {
			int v = vw.first;
			ll w = vw.second;
			if (dist[v] > w + d) {
				dist[v] = w + d;
				pq.push({dist[v], v});
			}
		}
	}
}
int main() {
//	freopen("in.txt", "r", stdin);
	scanf("%d %d", &n, &m);
	a.resize(n + 1);
	for (int i = 0; i < m; i++) {
		int u, v, c;
		scanf("%d %d %d", &u, &v, &c);
		a[u].push_back(ii(v, c));
	}
	clr(vis, false);
	ll dist[n + 1];
	for (int i = 1; i <= n; i++)
		dist[i] = LONG_LONG_MIN;
	int ss, f;
	cin >> ss >> f;
	tls(ss);
//	for (int i = 1; i <= n; i++)
//		if (!vis[i])
//			tls(i);
	reverse(all(s));
	dist[ss] = 0;
	for (int i = 0; i < sz(s); i++) {
		int u = s[i];
		for (int j = 0; j < sz(a[u]); j++) {
			int v = a[u][j].first;
			if (dist[u] == LONG_LONG_MIN)
				continue;
			dist[v] = max(dist[v], dist[u] + (long long) a[u][j].second);
		}
	}
	if (dist[f] == LONG_LONG_MIN)
		printf("No solution\n");
	else
		printf("%lld\n", dist[f]);
	return 0;
}
Example #18
0
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    while(cin >> n) {
        if(n == -1) return 0;

        adj2.assign(n, vi());
        energy.assign(n, 0);
        int k;
        memset(mp, 0, sizeof(mp));

        for(int i = 0; i < n; ++i) {
            cin >> energy[i] >> k;
            adj2[i].assign(k, 0);

            for(int j = 0; j < k; ++j) {
                cin >> adj2[i][j];
                --adj2[i][j];
                mp[i][adj2[i][j]] = 1;
            }
        }

        for(int k = 0; k < n; ++k)
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < n; ++j)
                    mp[i][j] |= mp[i][k] & mp[k][j];

        adj.assign(n, vii());
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < (int)adj2[i].sz(); ++j) {
                adj[i].pb(ii(adj2[i][j], -energy[adj2[i][j]]));
            }
        }

        dist.assign(n, INF);
        dist[0] = -100;
        for(int i = 0; i < n - 1; ++i)
            for(int u = 0; u < n; ++u)
                for(int j = 0; j < (int) adj[u].sz(); ++j) {
                    ii v = adj[u][j];
                    if(dist[u] + v.second < 0) {
                        dist[v.first] = min(dist[v.first], dist[u] + v.second);
                    }
                }

        if(dist[n - 1]  < 0) cout << "winnable\n";
        else {
            bool hasNeg = false;
            for(int u = 0; u < n; ++u) {
                if(mp[u][n - 1]) {
                    for(int j = 0; j < (int) adj[u].sz(); ++j) {
                        ii v = adj[u][j];
                        if(dist[u] + v.second < 0 && dist[v.first] > dist[u] + v.second) {
                            hasNeg = true;
                        }
                    }
                }
            }
            if(hasNeg) cout << "winnable\n";
            else cout << "hopeless\n";
        }
    }
}