POJ 1192 限制条件最短路
2013-08-18 16:42
399 查看
通道在一定的时间开放,处理一下就是最短路了
strtok(char[], " ")还是很好用的
引用一下百度百科
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
atof(将字符串转换成浮点型数)
atoi(将字符串转换成整型数)
atol(将字符串转换成长整型数)
strtod(将字符串转换成浮点数)
strtol(将字符串转换成长整型数)
strtoul(将字符串转换成无符号长整型数)
SFPA
strtok(char[], " ")还是很好用的
引用一下百度百科
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
for (i = 0, p = strtok(str, " "); p != NULL; ++i) { a[i] = atoi(p); p = strtok(NULL, " "); }
atof(将字符串转换成浮点型数)
atoi(将字符串转换成整型数)
atol(将字符串转换成长整型数)
strtod(将字符串转换成浮点数)
strtol(将字符串转换成长整型数)
strtoul(将字符串转换成无符号长整型数)
SFPA
#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const double PI = acos(-1.0); template <class T> inline T MAX(T a, T b){if (a > b) return a;return b;} template <class T> inline T MIN(T a, T b){if (a < b) return a;return b;} const int N = 111; const int M = 11111; const LL MOD = 1000000007LL; const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1}; const int INF = 0x3f3f3f3f; struct node { int w, next, v, ways; int ti[20][2]; }edge[1111]; int cnt, head[55]; int n, m, s, t; char str[1111]; int dist[55]; bool inq[55]; void add() { int a[45]; char *p; int i, j, k, u, v; for (i = 0, p = strtok(str, " "); p != NULL; ++i) { a[i] = atoi(p); p = strtok(NULL, " "); } edge[cnt].w = a[2]; edge[cnt].v = a[1]; edge[cnt].next = head[a[0]]; a[2] = 0; for (j = 3, k = 0; j < i; j +=2, ++k) { edge[cnt].ti[k][0] = a[j - 1]; edge[cnt].ti[k][1] = a[j]; } if (i == j) { edge[cnt].ti[k][0] = a[j - 1]; edge[cnt].ti[k][1] = INF; k++; } edge[cnt].ways = k; head[a[0]] = cnt++; edge[cnt] = edge[cnt - 1]; edge[cnt].v = a[0]; edge[cnt].next = head[a[1]]; head[a[1]] = cnt++; } void solve() { fill(dist, dist + n + 1, INF); memset(inq, false, sizeof(inq)); dist[s] = 0; queue < int > q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; int i, j, k; for (i = head[u]; i != -1; i = edge[i].next) { for (k = 0; k < edge[i].ways; ++k) { if (edge[i].ti[k][1] >= MAX(edge[i].ti[k][0], dist[u]) + edge[i].w) { if (MAX(edge[i].ti[k][0], dist[u]) + edge[i].w < dist[edge[i].v]) { dist[edge[i].v] = MAX(edge[i].ti[k][0], dist[u]) + edge[i].w; if (!inq[edge[i].v]) {inq[edge[i].v] = true; q.push(edge[i].v);} } } } } } if (dist[t] == INF) printf("*\n"); else printf("%d\n", dist[t]); } int main() { while (scanf("%d", &n) != EOF && n) { scanf("%d%d%d", &m, &s, &t); int i, u, v; cnt = 0; memset(head, -1, sizeof(head)); getchar(); for (i = 0; i < m; ++i) { gets(str); add(); } if (s == t) printf("0"); else solve(); } return 0; }
相关文章推荐
- POJ 1062 有限制条件的最短路
- POJ - 1724【有限制条件的最短路】+HDU- 3790【双重权值的最短路】
- poj 1724 有限制条件的最短路
- poj 1724 有限制条件的最短路
- POJ 2288 Islands and Bridges(状压DP->多种限制条件的TSP)
- POJ1724 ROADS(加限制条件的最短路,深搜)
- POJ 2392 Space Elevator(带限制条件的多重背包)
- POJ-1062 昂贵的聘礼 有限制的最短路
- poj 1062(有限制的最短路)
- POJ 3594 带限制的最短路 枚举+SPFA
- poj 2392背包条件限制
- POJ 1062 昂贵的聘礼(限制的最短路)
- Poj 1724 ROADS【二维限制最短路+SPFA】
- poj 1724(有限制的最短路)
- hdu 3790 最短路径问题(两个限制条件的最短路)
- POJ 1724 有限制的最短路
- poj 1724 ROADS 加限制的最短路
- poj 1724 ROADS条件限制下的最短路径
- poj 1724(有限制的最短路)
- poj 1724 ROADS条件限制下的最短路径