题意:有n个农场,每个农场有一头,所有的牛要去其中一个农场去开party,每条路每个方向牛走都需要一个时间,问所有的牛中来回之后用时最大的那一个时间最小。
开party的农场作为原点,跑spfa得到一组时间,反向建图跑spfa得到一组时间,两组相加的最大值即为答案
注意先存边再建图每次不要忘了init()
链接:poj 3268
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005;
const int maxm = 100500;
int n, m, s, t; //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
int d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep; //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0
int pre[maxn];
struct node {
int v, w, next;
}es[maxm];
void init() {
for(int i = 1; i <= n; i++) {
d[i] = inf;
inq[i] = false;
cnt[i] = 0;
head[i] = -1;
pre[i] = -1;
}
nodep = 0;
}
void addedge(int from, int to, int weight)
{
es[nodep].v = to;
es[nodep].w = weight;
es[nodep].next = head[from];
head[from] = nodep++;
}
bool spfa()
{
queue<int> que;
d[s] = 0; //s为源点
inq[s] = 1;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false; //从queue中退出
//遍历邻接表
for(int i = head[u]; i != -1; i = es[i].next) { //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输
//入的项,其next值为-1
int v = es[i].v;
if(d[v] > d[u] + es[i].w) { //松弛(RELAX)操作
d[v] = d[u] + es[i].w;
pre[v] = u;
if(!inq[v]) { //若被搜索到的节点不在队列que中,则把to加入到队列中去
inq[v] = true;
que.push(v);
if(++cnt[v] > n) {
return false;
}
}
}
}
}
return true;
}
void putpath() {
stack<int> path;
int now = t;
while(1) {
path.push(now);
if(now == s) {
break;
}
now = pre[now];
}
while(!path.empty()) {
now = path.top();
path.pop();
printf("%d\n", now);
}
}
int mp[maxn][maxn];
int d1[maxn], d2[maxn];
int main()
{
int T, kcase = 0;
while(cin >> n >> m >> s) {
memset(mp, inf, sizeof(mp));
int a, b, c;
while(m--) {
cin >> a >> b >> c;
if(mp[a][b] > c){
mp[a][b] = c;
}
}
init();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(mp[i][j] != inf) {
addedge(i, j, mp[i][j]);
}
}
}
if(spfa()) {
for(int i = 1; i <= n; i++) {
d1[i] = d[i];
//cout << d[i] << " ";
}
//cout << endl;
}
init();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(mp[i][j] != inf) {
addedge(j, i, mp[i][j]);
}
}
}
if(spfa()) {
for(int i = 1; i <= n; i++) {
d2[i] = d[i];
//cout << d[i] << " ";
}
//cout << endl;
}
int maxx = 0;
for(int i = 1; i <= n; i++) {
if(i != s && maxx < d1[i] + d2[i]) {
maxx = d1[i] + d2[i];
}
}
cout << maxx << endl;
}
return 0;
}
和上面的题意一样
不过数据很大 用结构体存图。
链接:poj 1511
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005000;
const int maxm = 1005000;
int n, m, s, t; //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
ll d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep; //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0
int pre[maxn];
struct node {
int u, v, next;
ll w;
}es[maxm], es1[maxm];
void init() {
for(int i = 1; i < maxn; i++) {
d[i] = inf;
inq[i] = false;
cnt[i] = 0;
head[i] = -1;
pre[i] = -1;
}
nodep = 0;
}
void addedge(int from, int to, ll weight)
{
es[nodep].v = to;
es[nodep].w = weight;
es[nodep].next = head[from];
head[from] = nodep++;
}
bool spfa()
{
queue<int> que;
d[s] = 0; //s为源点
inq[s] = 1;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false; //从queue中退出
//遍历邻接表
for(int i = head[u]; i != -1; i = es[i].next) { //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输
//入的项,其next值为-1
int v = es[i].v;
if(d[v] > d[u] + es[i].w) { //松弛(RELAX)操作
d[v] = d[u] + es[i].w;
//pre[v] = u;
if(!inq[v]) { //若被搜索到的节点不在队列que中,则把to加入到队列中去
inq[v] = true;
que.push(v);
if(++cnt[v] > n) {
return false;
}
}
}
}
}
return true;
}
int main()
{
int T, kcase = 0;
s = 1;
cin >> T;
while(T--) {
init();
cin >> n >> m;
int a, b;
ll c;
for(int i = 0; i < m; i++) {
scanf("%d %d %lld", &es1[i].u, &es1[i].v, &es1[i].w);
addedge(es1[i].u, es1[i].v, es1[i].w);
}
ll maxx = 0;
if(spfa()) {
for(int i = 1; i <= n; i++) {
maxx += d[i];
//cout << d[i] << " ";
}
//cout << endl;
}
init();
for(int i = 0; i < m; i++) {
addedge(es1[i].v, es1[i].u, es1[i].w);
}
if(spfa()) {
for(int i = 1; i <= n; i++) {
maxx += d[i];
//cout << d[i] << " ";
}
//cout << endl;
}
cout << maxx << endl;
}
return 0;
}