poj 3411 && 1724
题目:http://poj.org/problem?id=3411
题意:n 个城市,m 条路,每条路都有两种情况的费用,一种是 在 b 城市付费 r,一种是提前在 c 城市付费 p 元,问从 1 到 n 最小的花费
思路:邻接表 + dfs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <algorithm> 5 #define N 20 6 #define inf 100000000 7 #define _clr(a,val) (memset(a,val,sizeof(a))) 8 9 using namespace std; 10 11 int vis[N]; 12 int head[N]; 13 struct node 14 { 15 int a,b,c,p,r; 16 int next; 17 }point[N]; 18 int n,m; 19 int minn; 20 void dfs(int x,int sum) 21 { 22 if(sum > minn || vis[x] > n) return; // 当一个节点被访问n次时,就没必要再访问了 23 if(x == n) 24 { 25 if(sum < minn) minn = sum; 26 return ; 27 } 28 int t = head[x]; 29 while(t != -1) 30 { 31 vis[point[t].b] ++; 32 if(vis[point[t].c] > 0) dfs(point[t].b,sum + point[t].p); 33 else dfs(point[t].b , point[t].r + sum); 34 vis[point[t].b] --; 35 t = point[t].next; 36 } 37 } 38 int main() 39 { 40 int i; 41 //freopen("data.txt","r",stdin); 42 while(scanf("%d%d",&n,&m) != EOF) 43 { 44 _clr(vis,0); 45 _clr(head,-1); 46 for(i = 0; i < m; i++) 47 { 48 scanf("%d%d%d%d%d",&point[i].a,&point[i].b,&point[i].c,&point[i].p,&point[i].r); 49 point[i].next = head[point[i].a]; 50 head[point[i].a] = i; 51 } 52 minn = inf; 53 vis[1] = 1; 54 dfs(1,0); 55 if(minn == inf) printf("impossible\n"); 56 else printf("%d\n",minn); 57 } 58 return 0; 59 }
题目:http://poj.org/problem?id=1724
题意:n个城市 m 条路,每条路有两个参数,一个是长度,一个是费用,给出Bob拥有的钱,问从1 到 n在 可以付的起费的情况下的做短路长度
思路:用优先队列保存加进来的点(在不超过 Bob最多钱的情况下),然后bfs当找到n时就输出并退出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <queue> 5 #include <algorithm> 6 #define N 10010 7 #define inf 100000000 8 #define _clr(a,val) (memset(a,val,sizeof(a))) 9 10 using namespace std; 11 12 struct node 13 { 14 int point; 15 int len; 16 int cost; 17 friend bool operator < (node a,node b) // 定义顺序 18 { 19 return a.len > b.len; 20 } 21 }; 22 struct tnode 23 { 24 int f,t,l,c; 25 int next; 26 }eage[N]; 27 int s,d,r; 28 int head[N]; 29 void bfs() 30 { 31 node tem; 32 int flag = 0; 33 tem.point = 1, tem.len = tem.cost = 0; 34 priority_queue<node>qu; 35 qu.push(tem); 36 while(!qu.empty()) 37 { 38 //cout<<"999\n"; 39 node temp = qu.top(); 40 qu.pop(); 41 if(temp.point == d) 42 { 43 printf("%d\n",temp.len); 44 flag = 1; 45 break; //找到了就输出并退出 46 } 47 int i = head[temp.point]; 48 while(i != -1) 49 { 50 node ttem; 51 ttem.point = eage[i].t, ttem.len = temp.len + eage[i].l, ttem.cost = temp.cost + eage[i].c; 52 if(ttem.cost <= s) 53 { 54 qu.push(ttem); 55 } 56 i = eage[i].next; 57 } 58 } 59 if(!flag) printf("-1\n"); 60 return ; 61 } 62 int main() 63 { 64 int i; 65 //freopen("data.txt","r",stdin); 66 while(scanf("%d%d%d",&s,&d,&r) != EOF) 67 { 68 _clr(head,-1); 69 for(i = 0; i < r; i++) 70 { 71 scanf("%d%d%d%d",&eage[i].f,&eage[i].t,&eage[i].l,&eage[i].c); 72 eage[i].next = head[eage[i].f], head[eage[i].f] = i; 73 } 74 bfs(); 75 } 76 return 0; 77 }