\
Description
Longest Paths |
It is a well known fact that some people do not have their social abilities completely enabled. One example is the lack of talent for calculating distances and intervals of time. This causes some people to always choose the longest way to go from one place to another, with the consequence that they are late to whatever appointments they have, including weddings and programming contests. This can be highly annoying for their friends.
César has this kind of problem. When he has to go from one point to another he realizes that he has to visit many people, and thus always chooses the longest path. One of César's friends, Felipe, has understood the nature of the problem. Felipe thinks that with the help of a computer he might be able to calculate the time that César is going to need to arrive to his destination. That way he could spend his time in something more enjoyable than waiting for César.
Your goal is to help Felipe developing a program that computes the length of the longest path that can be constructed in a given graph from a given starting point (César's residence). You can assume that the graph has no cycles (there is no path from any node to itself), so César will reach his destination in a finite time. In the same line of reasoning, nodes are not considered directly connected to themselves.
A value of n = 0 indicates the end of the input.
After this, a second number s is provided, indicating the starting point in César's journey ( ). Then, you are given a list of pairs of places p and q, one pair per line, with the places on each line separated by white-space. The pair ``" indicates that César can visit q after p.
A pair of zeros (``0 0") indicates the end of the case.
As mentioned before, you can assume that the graphs provided will not be cyclic.
Print a new line after each test case.
2 1 1 2 0 0 5 3 1 2 3 5 3 1 2 4 4 5 0 0 5 5 5 1 5 2 5 3 5 4 4 1 4 2 0 0 0
Case 1: The longest path from 1 has length 1, finishing at 2. Case 2: The longest path from 3 has length 4, finishing at 5. Case 3: The longest path from 5 has length 2, finishing at 1.
但愿最短路径,不同的是,g[i][j]一开始赋值的是-1,此题用d[i] 来表示走过了几个节点
#include <cstdio> #include <string> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; #define INF 1 << 29 int g[155][155]; int d[155]; int n; int s; void Bellman() { int flag = 1; for (int i = 1; i <= n; i ++) d[i] = INF; d[s] = 0; for (int i = 1; i <= n - 1 && flag; i ++) { flag = 0; for (int j = 1; j <= n; j ++) for (int k = 1; k <= n; k ++) { if(g[k][j] && d[k] != INF && d[j] > d[k] + g[k][j]) { d[j] = d[k] + g[k][j]; flag = 1; } } } } int main () { int cas = 0; int u, v; while (scanf("%d", &n) != EOF && n) { memset(g, 0, sizeof(g)); ++ cas; scanf("%d", &s); while (scanf("%d%d", &u, &v) != EOF && u && v) { g[u][v] = -1; } Bellman(); int ans = 1; int tt = s; for (int i = 1; i <= n; i ++) { if(ans > d[i]) { ans = d[i]; tt = i; } } printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n", cas, s, -ans, tt); } return 0; }
flody :
#include <stdio.h> #include <string.h> int main() { int n, g[111][111], s, t, len, nT = 0; while(~scanf("%d", &n) && n){ scanf("%d", &s); memset(g, -1, sizeof(g)); int a,b; while(scanf("%d%d", &a, &b) && (a || b)) { g[a][b] = 1; } for(int k = 1; k <= n; ++k) for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) { if(g[i][k] != -1 && g[k][j] != -1 && (g[i][k] + g[k][j]) > g[i][j]) { g[i][j] = g[i][k] + g[k][j]; } } len = 0; for(int i = 1; i <= n; ++i) { if(g[s][i] > len) { len = g[s][i]; t = i; } } printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n", ++nT, s, len, t); } return 0; }
spfa
#include<cstdio> #include<iostream> #include<queue> #include<cmath> #include<cstring> #include<string> #include<algorithm> #include<stack> using namespace std; typedef long long LL; #define clr(x) memset(x,0,sizeof(x)); #define sf scanf #define pf printf #define INF 1<<29 const int maxn=105; int n; int map[maxn][maxn]; int dis[maxn]; bool vis[maxn]; int spfa(int s, int t){ queue<int>Q; while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); memset(dis, 0, sizeof(dis)); dis[s]=0; vis[s]=true; Q.push(s); while(!Q.empty()){ int now=Q.front(); Q.pop(); vis[now]=false; for(int i=1;i<=n;i++) { if(map[now][i]) { if(dis[now]+map[now][i]>dis[i]){ dis[i]=dis[now]+map[now][i]; if(!vis[i]){ vis[i]=true; Q.push(i); } } } } } return dis[t] ? dis[t] : -1; } int main(){ int nT = 0; int a, b; int s; while (scanf("%d", &n) != EOF && n) { memset(map, 0, sizeof(map)); scanf("%d", &s); while (scanf("%d%d", &a, &b) != EOF &&a && b) { map[a][b] = 1; } int len = 0; int flag = s; for (int i = 1;i <= n; i ++) { int sum = spfa(s, i); if(len < sum) { len = sum; flag = i; } } printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n", ++nT, s, len, flag); } return 0; }